TestNG XML example to execute with package names

In testng.xml file we can specify the specific package name (s) which needs to be executed.
In a project there may be many packages, but we want to execute only the selected packages.
The below is the example testng.xml which will execute the specific packages.
Example:

<?xml version="1.0" encoding="UTF-8"?>
<suite name="example suite 1" verbose="1" >
  <test name="Regression suite 1" >
    <packages>
      <package name="com.first.example" />
   </packages>
 </test>
</suite>

We need to specify the names of the packages in between the package tags.
In the above xml, we have specified package name as “com.first.example” which will get executed. It will execute all the classes which have TestNG annotations
Below are the two example classes under “com.first.example” package which is executed .

Classname: demoOne

package com.first.example;
import org.testng.annotations.Test;
public class demoOne {
	@Test
	public void firstTestCase()
	{
		System.out.println("im in first test case from demoOne Class");
	}
	@Test
	public void secondTestCase()
	{
		System.out.println("im in second test case from demoOne Class");
	}
}

Classname: demoTwo

package com.first.example;
import org.testng.annotations.Test;
public class demoTwo {
	@Test
	public void firstTestCase()
	{
		System.out.println("im in first test case from demoTwo Class");
	}
	@Test
	public void secondTestCase()
	{
		System.out.println("im in second test case from demoTwo Class");
	}
}

We need to run the testng.xml file. (Right click on testng.xml and select Run as ‘TestNG Suite”)

The below is the output for the above example code.

testng example"></p>
</body></html>

Test Frameworks: 

Comments

How to run only selected test cases from the testng.xml using ant.

Add new comment

CAPTCHA
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.