testng.xml file

What is testNG.xml?

A test suite is represented by one XML file which is tesng.xml.  It can contain one or more tests and is defined by the <suite> tag. A test is represented by <test> and can contain one or more TestNG classes. 

Following are the benefits of using testng.xml
  • We can define test suit using testng.xml file to run all test cases in a suit in one go.
  • Can Include or exclude test methods from software web application's test execution.
  • Can specify a group to Include or exclude.
  • Can pass parameter to use In test case of software web application.
  • Can specify group dependencies.
  • Can configure parallel test execution for software web application.
  • Can define listeners.
Following is the example of testng file.
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
<?xml version="1.0" encoding="UTF-8"?>
<suite name="example suite 1" verbose="1" >
  <test name="Regression suite 1" >
    <classes>
      <class name="Demo.demoOne"/>
      <class name="Demo.demoTwo"/>
      <class name="Demo.demoThree"/>
    </classes>
 </test>
</suite>


In above code,
<suite> : suite tag defines the TestNG test suite. You can give any name to your suite using 'name' attribute.

<test> : test tag defines the TestNG test. You can give any name to your test using 'name' attribute.

<classes> : classes tag defines multiple class. We can multiple test class under classes tag.

<class> : class tag defines the class name which you wants to consider in test execution.

Steps to create testng.xml:

  1. Right click your project -> New -> File
  2. Give name of your test suite file as testng.xml and click finish
  3. Write the code and save

Steps to execute testng.xml File:

Right click on testng.xml file -> Run As -> Select TestNG Suite


testng.xml file


Please see following guidelines of defining testng.xml.

1) To run all test cases using testng.xml.

1
2
3
4
5
6
7
8
<suite name="preserve-order testSuite">
    <classes>
      <class name="Demo.Test_1"/>
      <class name="Demo.Test_2"/>
      <class name="Demo.Test_3"/>
    </classes>
  </test>
</suite>


2) To run all tests in the order they are found in the testng.xml file. We have to set preserve-order attribute as true. If preserve-order attribute set as false then your classes or methods will run in an unpredictable order.
1
2
3
4
5
6
7
8
9
<suite name="preserve-order testSuite">
  <test name="preserve-orderTest" preserve-order="true">
    <classes>
      <class name="Demo.Test_1"/>
      <class name="Demo.Test_2"/>
      <class name="Demo.Test_3"/>
    </classes>
  </test>
</suite>

3) Configure testng.xml to run two classes in two different tests.
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
<suite name="TestSuite_1" >
 <test name="Test_1" >
  <classes>
   <class name="Demo.Class_1" />  
  </classes>
 </test> 
 <test name="Test_1" >
  <classes>
   <class name="Demo.Class_1" /> 
  </classes>
 </test> 
</suite>

4) Configure testng.xml to run all packages of project in single test suite.
1
2
3
4
5
6
7
<suite name="Suite_1">
 <test name="Test_1" >
  <packages>
   <package name=".*" />   
  </packages>
 </test> 
</suite>

5) Configure testng.xml to run selected packages from all packages of webdriver project in single test suite.
1
2
3
4
5
6
7
8
<suite name="Suite_1">
 <test name="Test_1" >
  <packages>
   <package name="Demo" />
   <package name="Demo_1" />
  </packages>
 </test> 
</suite>

6) Configuring testng.xml file to include only specific package in test suite from multiple packages.
1
2
3
4
5
6
7
8
9
<suite name="Suite_1">
  <test name="Test_1" >
     <packages>
        <package name=".*">
           <include name="Demo" />
        </package>
     </packages>
  </test>
</suite>

7) Configuring testng.xml file to exclude specific package from execution.
1
2
3
4
5
6
7
8
9
<suite name="Suite_1">
  <test name="Test_1" >
  <packages>
   <package name=".*">
    <exclude name="Demo_1" />
   </package>
  </packages>
 </test>
</suite>

8) Configure testng.xml file to include and run selected webdriver test methods from few classes.
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
<suite name="Suite_1">
  <test name="Test_1">
    <classes>
        <class name="Demo.Class_1">
          <methods>
             <include name="testmethod_1">
          </include></methods>
      <class name="Demo.Class_2">        
    </class></class></classes>
    </test> 
</suite>

9) Configure testng.xml file to exclude specific test method from class.
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
<suite name="Suite_1">
  <test name="Test_1" >
    <classes>
        <class name="Demo.Class_1" />
          <methods>
              <include name="testmethod_1" />
          </methods>
       <class name="Demo.Class_2" />
         <methods>
              <exclude name="testmethod_2" />
          </methods>        
    </classes>
    </test> 
</suite>

10) Including/Excluding test methods using regular expression.
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
<suite name="Suite_1">
  <test name="Test_2" >
  <classes>
   <class name="Demo.Class_1">
    <methods>
     <include name=".*Test.*"/>
    </methods>
   </class>
  </classes>
 </test>
</suite>

<-- Previous || Next -->

No comments:

Post a Comment