3

I have created a testng suite file. I am able to run it directly from Eclipse but want to know how to run it using command line on MAC OS X.

2 Answers2

3

To run testNG using command prompt in MAC OSX try below command -

java -cp "/opt/testng-6.8.jar:bin" org.testng.TestNG testng.xml

Make sure you have installed Java and its available from command prompt. Navigate to your folder where testNG.xml file is present and then run the above command. Give in the path where your testNG.jar file resides in the command, for me its /opt/testng-6.8.jar:bin.

Check this sample for more info. If you are getting JNI error with your testNG then include jcommader jar file path in the above command. Here's how -

java -cp "PATH/TO/TESTNG.jar:PATH/TO/jcommander-1.7.jar" org.testng.TestNG testng.xml

Hope this helps.

giri-sh
  • 6,934
  • 2
  • 25
  • 50
  • thank you very much for your reply. I have tried it but I got following error Error: JNI error has occured, please check your installation and try again – Arunkumar Aousula Sep 01 '15 at 09:34
  • There's an issue with testNG ([check here](https://github.com/cbeust/testng-eclipse/issues/140)) and so you are getting the JNI error. To resolve it check this [post](http://stackoverflow.com/questions/30112390/jni-error-has-occurred-please-check-your-installation) and update your command in command prompt to include the jcommander.jar file path. – giri-sh Sep 01 '15 at 10:14
  • @ArunkumarAousula updated answer to avoid JNI errors. Let me know if you still get any error. – giri-sh Sep 01 '15 at 10:25
  • 1
    it still shows an error `[TestNG] [ERROR] cannot find class in classpth: [class name]` this is what I am running `java -ea -classpath "./src/lib/testng-6.9.4.jar:./src/lib/jcommander-1.19.jar" org.testng.TestNG VODMobile_Login.xml` – Arunkumar Aousula Sep 01 '15 at 10:39
  • 1
    I am always getting Error: Could not find or load main class org.testing.TestNG even though I can see it into my testng.jar. Does any one why it happens? – Loebre Nov 16 '15 at 10:55
1

Paths are separated using : under Unix-like systems and not ; as in Windows:

java -cp ./src/lib/*:./bin org.testng.TestNG testng.xml

If you are using bash under Windows then use ; or if unix/linux then :

The ; character means end of statement to a Unix shell, so what you are attempting to exceute is:

java -cp ./src/lib/*
./bin org.testng.TestNG testng.xml
Shubham Jain
  • 16,610
  • 15
  • 78
  • 125
  • Hi Shubhnam Jain, thank u for ur reply. I have tried your code my class files are in this location `./target/test-classes/tv/digisoft/VODMobile` this is what I am running `java -cp ./src/lib/*:./target/test-classes/tv/digisoft/VODMobile org.testng.TestNG VODMobile_Login.xml` I got an error as `Could not find class in classpath: com.appium.test.test_login`. – Arunkumar Aousula Sep 01 '15 at 09:49
  • Try this java -cp ./src/lib/*:./target/test-classes org.testng.TestNG VODMobile_Login.xml – Shubham Jain Sep 01 '15 at 10:02
  • Or try this java -cp ./src/lib/*:./target org.testng.TestNG VODMobile_Login.xml – Shubham Jain Sep 01 '15 at 10:03
  • I have tried this and got this result `java -cp ./src/lib/*:./target/test-classes org.testng.TestNG VODMobile_Login.xml [TestNG] Running: /Users/dan/VOD_Android/VODMobile_Login.xml =============================================== Suite Total tests run: 2, Failures: 0, Skips: 2 Configuration Failures: 2, Skips: 0 ===============================================` – Arunkumar Aousula Sep 01 '15 at 10:11
  • is you are facing same issue with IDE also? Go to your IDE right click on VODMobile_Login.xml and run as "Testng" – Shubham Jain Sep 01 '15 at 10:14
  • I can run it using eclipse. It is fine over there. The problem is only with command line. – Arunkumar Aousula Sep 01 '15 at 10:15
  • It is some how skipping the tests – Arunkumar Aousula Sep 01 '15 at 10:16
  • Is your build path libraries and libraries on location ./src/lib/* is different?? – Shubham Jain Sep 01 '15 at 10:41
  • yes it is different from ./src/lib/* it is in this location `/Users/dan/.m2/repository/` – Arunkumar Aousula Sep 01 '15 at 10:44
  • Then check that you have updated jars of all libraries because TestNg is now executing fine for you through terminal. As you can see now there is no such error from TestNg came from. It's all about your configuration – Shubham Jain Sep 01 '15 at 10:49
  • @Shubhnam I have in two ways `java -cp /Users/dan/.m2/repository/org/testng/testng/6.9.4/*:./target/test-classes/ org.testng.TestNG VODMobile_Login.xml` I got this error `Error: Could not find or load main class org.testng.TestNG` If I try in another way `java -cp /Users/dan/.m2/repository/org/testng/testng/6.9.4/*:./target/test-classes/ org.testng.TestNG VODMobile_Login.xml ` i got this following error `Error: A JNI error has occurred, please check your installation and try again` – Arunkumar Aousula Sep 01 '15 at 10:57
  • did you try another one-->> java -cp ./src/lib/*:./target org.testng.TestNG VODMobile_Login.xml – Shubham Jain Sep 01 '15 at 11:12
  • Yes I have tried it but it still skips the tests `[TestNG] Running: /Users/dan/VOD_Android/VODMobile_Login.xml =============================================== Suite Total tests run: 2, Failures: 0, Skips: 2 Configuration Failures: 2, Skips: 0 ===============================================` – Arunkumar Aousula Sep 01 '15 at 11:15
  • Bingo .. I told you there is any configuration issue left only ... :) .. njoy :D – Shubham Jain Sep 01 '15 at 13:56
  • If my post help you then please upvote :) .. it will help :) – Shubham Jain Sep 01 '15 at 13:58