testng.xml File with TestNG

In any project, you will end up to a place where you need to execute so many test cases on a run. Running a set of test cases together is call executing a Test Suite. Those test cases can be dependent to each other or may have to be executed in a certain order.

The main advantage of using TestNG framework in Selenium is its ease of running multiple tests from multiple classes using just one configuration

In TestNG framework, we need to create testng.xml file to create and handle multiple test classes. Testng.xml is an XML file that describes the runtime definition of a test suite. It describes complex test definition while still remain easy to edit.

Using Testng.xml file we can run test available in one or more class file and make them as a single group, hence making test more meaningful, we call them as scenario based testing.

  1. Can I run test without using testng.xml file?

Ans-Well answer is yes, you can run. But once your test become larger and requires configuration of multiple test at one place, the best option is testng.xml.

The testng.xml file will have following hierarchy

  • The root tag of this file is <suite>.
  • <suite> tag can contain one or more <test> tags.
  • <test> tag can contain one or more <classes> tags.
  • <classes> tag can contain one or more <method> tags.

Let’s implement testng.xml File in your project:

Creating testng.xml File

To create testng.xml file, Right click on project folder and Go to New -> File as shown in bellow given image. It will open New File wizard.

In New file wizard, select “DemoTestNG” project and add file name = “testng.xml” as shown in bellow given image and click on Finish button.

It will add testng.xml file under your project folder. Now add bellow given lines in your testng.xml file.

<suite name="Suite One" >
 <test name="Test One" >
  <classes>
   <class name="TestNPack1.Program1" />
  </classes>
 </test>
</suite>

Now your testng.xml file will looks like bellow.

testng.xml code Explanation:

<suite> : suite tag defines the TestNG suite. You can give any name to your suite using ‘name’ attribute. In above given example, We have given “Suite One” to our test suite.

<test> : test tag defines the TestNG test. You can give any name to your test using ‘name’ attribute. In above given example, We have given “Test One” to our test.

<classes> : classes tag defines multiple class. We can multiple test class under classes tag. In our example we have only one class.

<class> : class tag defines the class name which you wants to consider in test execution. In above given example, we have defined name=”TestNGPack1.Program1″ where ‘TestNGPack1’ describes package name and ‘ Program1’ describes class name.

So the above given testng.xml file will execute only Program1 class from TestNGPack1package.

Executing testng.xml File

To Run

Right click on testng.xml file -> Run As -> Select TestNG Suite as shown in bellow given image.

It will start execution of defined software test class ‘Program1’ from ‘TestNGPack1’ package.

 

Execute Multiple Classes and Multiple Test using testng.xml File

In testng.xml file we can specify multiple names which need to be executed.
In a project there may be many classes, but we want to execute only the selected classes.

If say suppose, you have two/multiple classes in your test suite then how will you run them? We can run both the classes in same test as well in 2 different tests.

Let’s understand this with below example.

First of all, Create 3 classes under project = DemoTestNG and package = TestNGPack1 as bellow.

BaseClass” will be used for initializing and closing WebDriver instance,

Program1” and “Program2” will be used as test classes.

BaseClass.Java

package TestNGPack1;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.annotations.AfterSuite;
import org.testng.annotations.BeforeSuite;

public class BaseClass {
	//Declared as public static to use same webdriver instance publicly
	public static WebDriver driver = new FirefoxDriver();

	//@BeforeSuite annotation describes this method has to run before all suites
	@BeforeSuite
	public void setup() throws Exception {
		driver.manage().window().maximize();
		driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
		driver.get("http://www.testingbar.com/");
	}
	//@AfterSuite annotation describes this method has to run after execution of all suites
	@AfterSuite
	public void tearDown() throws Exception {
		driver.quit();
	}
}

Above class will be used as base class to initialize and close WebDriver instance.

Program1.Java

package TestNGPack1;
import org.testng.annotations.Test;

public class Program1 extends BaseClass {
	//@Test annotation describes this method as a test method
	@Test
	public void testmethod1() {
		String title = driver.getTitle();
		System.out.print("\nCurrent page title is : " + title);
		String Workdir = System.getProperty("user.dir");
		String Classpackname = this.getClass().getName();
		System.out.print("\n'" + Workdir + " -> " + Classpackname + " -> testmethod1' has been executed successfully");
	}
}

Above Program1 is inherited from BaseClass.

Program2.java

package TestNGPack1;
import org.testng.annotations.Test;

public class Program2 extends BaseClass {
	//@Test annotation describes this method as a test method
	@Test
	public void testmethod2() {
		driver.navigate().to("http://google.com/");
		String title = driver.getTitle();
		System.out.print("\nCurrent page title is : " + title);
		String Workdir = System.getProperty("user.dir");
		String Classpackname = this.getClass().getName();
		System.out.print("\n'" + Workdir + " -> " + Classpackname + " -> testmethod2' has been executed successfully");
	}
}

Now you are done with Class Files setup. It’s the time to configure testng.xml File.

Configure testng.xml to run two classes in one test

Copy-paste the below code in your testng.xml file and then Run it as TestNg Suite. Here, both classes are included in single test (Test One).

<suite name="Suite One" >
 <test name="Test One" >
  <classes>
   <class name="TestNGPack1.Program1" />
   <class name="TestNGPack1.Program2" /> 
  </classes>
 </test> 
</suite>

Once execution completed the execution result reports will looks like this.

Configure testng.xml to run two classes in two tests

Copy-paste the below code in your testng.xml file and then Run it as TestNg Suite. Here, both classes are included in separate test.

Here, “Program1” is included in ‘Test One” test and “Program2” is included in ‘Test Two” test.

<suite name="Suite One" >
 <test name="Test One" >
  <classes>
   <class name="TestNGPack1.Program1" />  
  </classes>
 </test> 
 <test name="Test Two" >
  <classes>
   <class name="TestNGPack1.Program2" /> 
  </classes>
 </test> 
</suite>

Once execution completed the execution result reports will looks like this.

Now compare both the results. In 1st example, both the classes are executed under same test but in 2nd example both classes are executed under separate tests. This way you can configure testng.xml file as per your requirement.

 

How to Execute Multiple Classes from Different Packages:

We have already seen example of how to configure testng.xml file to run single/multiple classes of same package.
In this post we will see how to execute multiple classes from different packages.

First of all let’s create new packages and classes as described in below steps.

Step 1: Create package = “TestNGPack1” and under this package Create classes as Program1.java and Program2.java

Step 2: Create package = “TestNGPack2” and under this package Create classes as Program3.java and Program4.java

Once done with above steps your Project structure will look like this.

Program1.java

package TestNGPack1;

import org.testng.annotations.Test;

public class Program1 {
	//@Test annotation describes this method as a test method
	@Test
	public void TC001() {
		System.out.println("====TC001 Executed====");
	}
}

Program2.java

package TestNGPack1;
import org.testng.annotations.Test;
public class Program2 {
	//@Test annotation describes this method as a test method
	@Test
	public void TC002() {
		System.out.println("====TC002 Executed====");
	}
}

Program3.java

package TestNGPack2;
import org.testng.annotations.Test;
public class Program3 {
	//@Test annotation describes this method as a test method
	@Test
	public void TC003() {
		System.out.println("====TC003 Executed====");
	}
}

Program4.java

package TestNGPack2;
import org.testng.annotations.Test;
public class Program4 {
	//@Test annotation describes this method as a test method
	@Test
	public void TC004() {
		System.out.println("====TC004 Executed====");
	}
}

Scenario: 1
How to execute all the classes of all the packages in one test?

Ans: You need to configure your testng.xml file as bellow.

Copy-paste the below code in your testng.xml file and then Run it as TestNg Suite. Here, all classes are included in single test (Test One).

<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >
<suite name="Suite One" >
 <test name="Test One" >
  <classes>
   <class name="TestNGPack1.Program1" />
   <class name="TestNGPack1.Program2" /> 
   <class name="TestNGPack2.Program3" /> 
   <class name="TestNGPack2.Program4" /> 
   </classes>
 </test> 
 </suite>

Once execution completed the execution result reports will looks like this.

Scenario: 2
How to execute selected classes (Let’s say Program1 and Program3) of 2 different packages in separate test?

Ans: You need to configure your testng.xml file as bellow.

Copy-paste the below code in your testng.xml file and then Run it as TestNg Suite. Here, all classes are included in single test (Test One).

<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >
<suite name="Suite One" >
 <test name="Test One" >
  <classes>
   <class name="TestNGPack1.Program1" />
   </classes>
 </test> 
 <test name="Test two" >
  <classes>
   <class name="TestNGPack2.Program3" /> 
   </classes>
 </test> 
 </suite>

Once execution completed the execution result reports will looks like this.

 

 

How to execute All Or Specific Packages

In testng.xml file, You can also configure to run specific package or all packages of a project.

Let’s consider that your Project Structure is as below.

Step 1: Create package = “TestNGPack1” and under this package Create classes as Program1.java and Program2.java

Step 2: Create package = “TestNGPack2” and under this package Create classes as Program3.java and Program4.java

Once done with above steps your Project structure will look like this.

Program1.java

package TestNGPack1;

import org.testng.annotations.Test;

public class Program1 {
	//@Test annotation describes this method as a test method
	@Test
	public void TC001() {
		System.out.println("====TC001 Executed====");
	}
}

Program2.java

package TestNGPack1;
import org.testng.annotations.Test;
public class Program2 {
	//@Test annotation describes this method as a test method
	@Test
	public void TC002() {
		System.out.println("====TC002 Executed====");
	}
}

Program3.java

package TestNGPack2;
import org.testng.annotations.Test;
public class Program3 {
	//@Test annotation describes this method as a test method
	@Test
	public void TC003() {
		System.out.println("====TC003 Executed====");
	}
}

Program4.java

package TestNGPack2;
import org.testng.annotations.Test;
public class Program4 {
	//@Test annotation describes this method as a test method
	@Test
	public void TC004() {
		System.out.println("====TC004 Executed====");
	}
}

Configure testng.xml to run selected packages from all packages of “DemoTestNG” project in single test suite.

<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >
<suite name="Suite One" >
 <test name="Test One" >
 	<packages>
   		<package name="TestNGPack2" />
   </packages>
 </test> 
 </suite>

In above testng.xml code, <packages> tag is used to describe group of packages and <package> tag package is used to add specific package in our test suite. 

So when you execute above testng.xml file, it will run only targeted packages. Here I have given only one package. i.e TestNGPack2

You can add more packages according to your requirements.

Once execution completed the execution result reports will looks like this.

As you see, “TestNGPack1” package is not executed as shown in above report image.

Configure testng.xml to run all packages of project in single test suite

If you wants to run all packages of your project, you can configure your testng.xml using wildcard(.*) with package tag as bellow.

<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >
<suite name="Suite One" >
 <test name="Test One" >
 	<packages>
   		<package name=".*" />
   </packages>
 </test> 
 </suite>

Once execution completed the execution result reports will looks like this.

As you see in report, now all the packages are executed. This way, we can use wild card to include all the test packages in our test.

 

Leave a Reply

Your email address will not be published. Required fields are marked *