Adding Methods to testng.xml file
Adding methods to testng.xml file, will explain you about creating and configuring TestNG test suite with methods which are developed/created in the java project. A Class may contain one or more methods in it. With this testng.xml configuration file we can include all the methods belongs to a particular class to the test suite.
Below are The Steps for Adding Methods to testng.xml file:
1.Create a java project with multiple methods in each class.
In the above project, created a class called “FirstClassInFirstPackage” with three methods called “firstTestCase”, “secondTestCase” and “thirdTestCase”.
2.Below is the sample code for the above test class and methods.
package com.test.firstpackage; import org.testng.annotations.Test; public class FirstClassInFirstPackage { @Test public void firstTestCase() { System.out.println("First TestNG test in First Package"); } @Test public void secondTestCase() { System.out.println("Second TestNG test in First Package"); } @Test public void thirdTestCase() { System.out.println("Third TestNG test in First Package"); } }
3.Now create a testng.xml file (here the xml file name is “testng-methods.xml”) with below code/configuration content. To work with methods in the testng.xml file, we have to use include or exclude tags in the class tag.
Include meas which methods to execute and exclude means which methods NOT to execute from a particular class.
If we want to execute all the methods from a particular class then we can directly use the entire class to execute from the configuration file. Which we have seen the Adding Classes to testng.xml file.
But particularly, when we want to include or exclude some of the test methods then we can go for methods tag and should use the include or exclude tags to execute particular test methods.
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd"> <suite name="Suite"> <test name="ClassTest"> <classes> <class name="com.test.firstpackage.FirstClassInFirstPackage"> <methods> <include name="firstTestCase"></include> <include name="secondTestCase"></include> <exclude name="thirdTestCase"></exclude> </methods> </class> </classes> </test> </suite>
4.Now execute the “testng-methods.xml” file from eclipse as TestNG suite. Then you can see the following output.
In the above output, we can not see the “thirdTestCase” in the test result as this is excluded from the configuration file.
5.We have multiple test methods in a test class and only want to execute single test method from the class, then we can include only that test method in the configuration file and no need to mention the exclude for the other test methods.
Below is the sample code :
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd"> <suite name="Suite"> <test name="ClassTest"> <classes> <class name="com.test.firstpackage.FirstClassInFirstPackage"> <methods> <include name="firstTestCase"></include> </methods> </class> </classes> </test> </suite>
6.This is the output for the above code:
In the above output, we can see only one test method executed and remaining are exclude from the execution.
7.Now we want to exclude only one method and remaining should include in the execution. Then we just mention one exclude tag with the particular method. This should include other methods indirectly.
Below is the sample code :
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd"> <suite name="Suite"> <test name="ClassTest"> <classes> <class name="com.test.firstpackage.FirstClassInFirstPackage"> <methods> <exclude name="firstTestCase"></exclude> </methods> </class> </classes> </test> </suite>
8.This is the output for the above code:
In the above output, we can see all test method executed except one which we mentioned in the configuration file.
This way we can configure the methods in the testng xml configuration file to execute the scripts as methods and can analyse the test results.