Basics of testng.xml is covered in last post, now let’s see different ways to run testng.xml
Using code editor pane
In Eclipse / InteliJ editor, right click on the project, select Run As -> TestNG Test
or
You can run specific testNG test methods, by clicking on the green play icon left to each @Test method
or
You can run all tests by clicking on the double play icon left to the class signature.
Using command line
We can use maven sure-fire plugin to specify the testng.xml file, when we run mvn test in command line, testng.xml file will be picked from pom.xml file to run the tests.
pom.xml
<plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-surefire-plugin</artifactId> <version>3.0.0-M5</version> <configuration> <suiteXmlFiles> <suiteXmlFile>testng.xml</suiteXmlFile> </suiteXmlFiles> <includes> <include>*.*</include> </includes> <properties> <property> <name>listener</name> <value>testNgLearning.listeners.TestNGListenerManager</value> </property> </properties> </configuration> </plugin> </plugins>
You can specify the path of testng.xml file inside the suiteXmlFile element.
Also you can specify the listeners like IReporter implemented user class or ITestListener implemented class inside the listener property element / tag of pom.xml
Now open terminal or command prompt > navigate to project directory > run using below command
mvn test
This above command will run the tests specified inside the testng.xml.
Using Java code
You can use TestNG class object to run the specified testng.xml file
You can write below code inside a main method to run.
List<String> xmlFile = new ArrayList<String>(); xmlFile.add("./testng.xml"); //we can even more than one testng.xml files to xmlFile array to run xmlFile.add("./testng_regresstion.xml"); TestNG tng = new TestNG(); tng.setTestSuites(suites); tng.run();
Using ANT build
Refer post: Selenium testNG ANT reporting
As you know now how to run single testng.xml, you might be interested to know how to run multiple testng.xml files at once.
1 Comment