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);
}
}
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);
}
}
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();
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
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!
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
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>