The testng.xml file allows you to set up a general configuration for our tests that follow this hierarchy:
<suite>
<test>
<classes>
<class name="com.sample.SampleTestOne" />
<class name="com.sample.SampleTestTwo" />
</classes>
</test>
</suite>
This hierarchy can be explained as follows:
<suite>: This is the major entity which groups everything and can contain one or more tests.
<test>: This represents a set of Java packages or classes that can be logically grouped since the feature/features being tested are conceptually alike.
<class>: This represents the Java class that contains one or more methods that will be included in the test.
<package>: This represents the Java package that contains one or more classes that will be included in the test.
<method>: This represents a list of methods that will be included in the test.
<groups>: This represents a declaration of group names that will be considered (or not) during the execution of the test suite.
If we are performing tests on a single Java class (contained in a single Java file), adding a TestNG annotation to it should be sufficient to automate that given test. However, there will be cases in which tests involve more than a single Java class and perhaps more than one package. Depending on the purpose, we can have different type of testng.xml files. Let's look at a few examples:
- We could specify every single class contained in our test It is also possible to specify the packages those classes belong to, like so:
<suite>
<test name="LoginTests">
<packages>
<package name="com.sample.login" />
</packages>
</test>
<test name="SearchTests">
<packages>
<package name="com.sample.search" />
</packages>
</test>
</suite>
»» We want to exclude/include groups in a test:
<suite>
<test name="SmokeTests">
<groups>
<run>
<exclude name="broken" />
<include name="smoke" />
</run>
</groups>
<classes>
<class name="com.smoke.SmokeOne" />
<class name="com.smoke.SmokeTwo" />
</packages>
</test>
</suite>
»» We want to explicitly declare the methods to be executed for a given
class:
<suite>
<test name="RegisterTests">
<classes>
<class name="com.register.RegisterOne" />
<class name="com.register.RegisterTwo" />
<methods>
</class>
</packages>
</test>
</suite>
More detailed and up-to-date information about the testng.xml file can be found at http://testng.org/doc/documentation-main.html#testng-xml.