10

I have testng.xml file created. Is there any way to run this file from java main method?

Something like -

Class test {
  public static void main ( String [ ] args) 
  {
    Run(testng.xml);
  }
}
WiSaGaN
  • 46,887
  • 10
  • 54
  • 88
Deepak
  • 336
  • 1
  • 3
  • 10
  • Could you please explain what you are trying to achieve by running testng.xml ? – Reji Apr 24 '14 at 16:09
  • 1
    In eclipse to run tests I am running testng.xml by right clicking it & then selecting option run as testng suite. I want to make a jar and want to run it after certain intervals. Thats why trying to invoke testng.xml from main method – Deepak Apr 24 '14 at 16:39

5 Answers5

20

You can run testng directly from commandline, probably make a bat file on top of it or use jenkins to trigger it. Refer here

or

If you want it in main, then you can try

TestListenerAdapter tla = new TestListenerAdapter();
TestNG testng = new TestNG();
List<String> suites = Lists.newArrayList();
suites.add("c:/tests/testng1.xml");//path to xml..
suites.add("c:/tests/testng2.xml");
testng.setTestSuites(suites);
testng.run();
niharika_neo
  • 8,441
  • 1
  • 19
  • 31
  • Exported runnable jar file with testng.xml & tried from CMD prompt, always getting not found xml. But when i use with Class name explicitly, able to run test from CMD. Can you help me out from this? [Note: have not used ANT, MAVEN] – saravana Apr 10 '17 at 08:59
  • 1
    Hello niharika, I think a URL is missed in the first approach. You intended to provide URL for creating bat file on top of jar file but there is no link inserted. I'm in need of that solution. Can you provide that link? – learner Jul 05 '18 at 04:50
  • @learner https://www.oreilly.com/library/view/testng-beginners-guide/9781782166009/ch02s05.html – Saikat Dec 19 '18 at 05:49
6

Please try below code and make sure you have testNG jar added in your jar manifest file.

public static void main(String[] args)
{
    org.testng.TestNG.main(args);
}

now you can pass all the parameters to your jar which are same for testNG jar file.

e.g.

java -jar yourjar.jar testng.xml

or

java -jar yourjar.jar -testclass org.test.xyz.java

Himanshu
  • 648
  • 4
  • 11
4

This work for me. More details here.

// Create object of TestNG Class
TestNG runner=new TestNG();

// Create a list of String 
List<String> suitefiles=new ArrayList<String>();

// Add xml file which you have to execute
suitefiles.add("C:\\Automation Test\\Git\\vne_automation\\testng.xml");

// now set xml file for execution
runner.setTestSuites(suitefiles);

// finally execute the runner using run method
runner.run();

Hope this helps!

Huy Hóm Hỉnh
  • 597
  • 7
  • 18
  • i get no ClassPath found when i try to run this. My testng.xml contains the list of all the classes from different packages of same project which are present inside /test/java folder. – just a learner Sep 08 '21 at 10:24
1

All the answers above works like a charm but there is one limitation that it would run unless and untill you remove the scope tag in pom.xml

In pom.xml there would be maven dependancy for testNg and in there there would be one scope tag which limits our jar to only upto test

Just remove that one

Saikat
  • 14,222
  • 20
  • 104
  • 125
abishek kachroo
  • 35
  • 1
  • 11
0

I didn't get any error and XML path is correct. But It just end execution with failed test cases. It does not open firefox browser. I am using testng annotation (i.e. @Before Suite,@Test, @group etc ) inside classes whereas it successfully execute using Testng Suite in eclipse.

<!--  suite name="Example"  parallel="methods" thread-count="2" parallel="false" preserve-order="true"> -->
<suite name="Example Donate System" verbose='1'>

    <test name="Agency"   >
    <packages>
      <package name="com.Donatesystem.AgencyController" />
   </packages>
 </test>
</suite>