Reading:  

Working with TestNG - Starters Guide


Group Tests

A group is one more annotation of TestNG which can be used in execution of multiple tests.Group annotation is a new added feature in TestNG, which doesn’t exist in JUnit framework.

User can specify any number of groups, and that group may have sub group or any other group inside it.By using TestNG tag can include a group, and we can also exclude a group.

Groups are specified in testng.xml file using the <groups> tag. Probably like to run all the grouped tests together in a batch. 

Example:

Create a class named as GroupExample.java

Here two methods for car, two methods of bike

package testNG;

import org.testng.annotations.Test;

public class GroupExample {

	@Test(groups = {
		"car"
	})
	public void car1() {
		System.out.println("Batch car-Test Car1");
	}

	@Test(groups = {
		"car"
	})
	public void car2() {
		System.out.println("Batch car-Test Car2");
	}

	@Test(groups = {
		"Bike"
	})
	public void bike1() {
		System.out.println("Batch Bike-Test bike1");
	}

	@Test(groups = {
		"Bike"
	})
	public void bike2() {
		System.out.println("Batch Bike-Test bike2");
	}
}

Create testing.xml file include the only group car as shown below

<?xml version="1.0" encoding="UTF-8"?>
<suite name="test Suite">
<test name="Practice Grouping">
<groups>
<run>
<include name="car"/>
</run>
</groups>
<classes>
<class name="testNG.GroupExample"/>
</classes>
</test>
</suite>

Run the testing.xml file by click on run button in eclipse

You will get this output

 

Group of Groups

The group may include another group is called MetaGroup. Let’s see how to handle groups of groups.

package testNG;

import org.testng.annotations.Test;

public class GroupExample {

	@Test(groups = {
		"car"
	})
	public void car1() {
		System.out.println("Batch car-Test Car1");
	}

	@Test(groups = {
		"car"
	})
	public void car2() {
		System.out.println("Batch car-Test Car2");
	}

	@Test(groups = {
		"Bike"
	})
	public void bike1() {
		System.out.println("Batch Bike-Test bike1");
	}

	@Test(groups = {
		"Bike"
	})
	public void bike2() {
		System.out.println("Batch Bike-Test bike2");
	}@Test(groups = {
		"car", "Sedancar"
	})
	public void sedan1() {
		System.out.println("Batch Car-Test Sedan1");
	}
}

Create testing.xml file include all group and sub group as shown below

<?xml version="1.0" encoding="UTF-8"?>
<suite name="test Suite">
<test name="Practice Grouping">
<groups>
<define name="all">
<include name="car"/>
<include name="Bike"/>
</define>
<run>
<include name="all"/>
</run>
</groups>
<classes>
<class name="testNG.GroupExample"/>
</classes>
</test>
</suite>

Run the testing.xml file by click on run button in eclipse, you will get the output below.

Group of Groups TestNG

Exclusion Groups:

By using <exclude> tag we can ignore a group as shown below:

<?xml version="1.0" encoding="UTF-8"?>
<suite name="test Suite">
<test name="Practice Grouping">
<groups>
<define name="all">
<exclude name="car"/>
<include name="Bike"/>
</define>
<run>
<include name="all"/>
</run>
</groups>
<classes>
<class name="testNG.GroupExample"/>
</classes>
</test>
</suite>

Run the testing.xml file by click on run button in eclipse, you will get the output below.

As you can see that that car tests were excluded by testNG

Description

This tutorial is focused on getting you started on TestNG, the testing framework inspired from JUnit and NUnit. Here is a quick table of contents

  1. What is TestNG?
  2. Environment Set-up
  3. Writing Tests
  4. Basic Annotations
  5. Execution Procedure
  6. Executing Tests
  7. Suite Test
  8. Ignore Test
  9. Group Test
  10. Exception Test
  11. Dependency Test
  12. Parametrized Test
  13. JUnit Tests
  14. Test Reports
  15. Running tests without Eclipse
  16. Plugin with ANT

 

 



Environment

A computer capable of running Java. IntelliJ Community IDE is also required for this course.

Prerequisites

Good knowledge of Java programming language and eclipse is essential.

Audience

Students looking to get started with TestNG

Learning Objectives

This tutorial will get you started with TestNG.

Author: Subject Coach
Added on: 12th Mar 2015

You must be logged in as Student to ask a Question.

None just yet!