Run Test in Groups using TestNG

how to run group of tests using TestNG

106

TestNG provides an option to run the tests in groups. Even one class which contains a number of test cases ( Test methods) can be run into same or different groups.

TestNG also provides annotations called @BeforeGroups and @AfterGroups. let’s build a scenario!

Scenario: we will create a test class with 4 different test methods belongs to two different groups and each group will have before and after group methods. one test method will belong to both the groups.

 

package TestNG;

import org.testng.annotations.AfterGroups;
import org.testng.annotations.BeforeGroups;
import org.testng.annotations.Test;

public class TestNGGroups {
 
 @BeforeGroups("group 1")
 public void BeforeGroup1() {
 System.out.println("BeforeGroup1 - beofore executing group 1 test cases");
 }
 
 @AfterGroups("group 1")
 public void AfterGroup1() {
 System.out.println("AfterGroup1 - after executing group 1 test cases");
 }
 
 @BeforeGroups("group 2")
 public void BeforeGroup2() {
 System.out.println("BeforeGroup2 - beofore executing group 2 test cases");
 }
 
 @AfterGroups("group 2")
 public void AfterGroup2() {
 System.out.println("AfterGroup2 - after executing group 2 test cases");
 }
 
 @Test(groups= "group 1")
 public void test1Group1() {
 System.out.println("test1Group1 - this test will execute in group 1");
 }

@Test(groups= "group 1")
 public void test2Group1() {
 System.out.println("test2Group1 - this test will execute in group 1");
 }
 
 @Test(groups = "group 2")
 public void test1Group2() {
 System.out.println("test1Group2 - this test will execute in group 2");
 }

@Test(groups = "group 2")
 public void test2Group2() {
 System.out.println("test2Group2 - this test will execute in group 2");
 }
 
 @Test(dependsOnGroups = {"group 1","group 2"})
 public void bothGroups() {
 System.out.println("bothGroups - will execute in both groups i.e. group 1 and group 2");
 }
}

 

output:

[RemoteTestNG] detected TestNG version 6.14.3
BeforeGroup1 - beofore executing group 1 test cases
test1Group1 - this test will execute in group 1
test1Group2 - this test will execute in group 2
AfterGroup1 - after executing group 1 test cases
BeforeGroup2 - beofore executing group 2 test cases
test2Group1 - this test will execute in group 1
test2Group2 - this test will execute in group 2
AfterGroup2 - after executing group 2 test cases
bothGroups - will execute in both groups i.e. group 1 and group 2
PASSED: test1Group1
PASSED: test1Group2
PASSED: test2Group1
PASSED: test2Group2
PASSED: bothGroups

===============================================
 Default test
 Tests run: 5, Failures: 0, Skips: 0
===============================================


===============================================
Default suite
Total tests run: 5, Failures: 0, Skips: 0
===============================================

make sure that test execution should be in sequence.

Subscribe to Newsletter
Get Latest Posts, Articles and Learning Lessons on Software Testing and Test Automation
You can unsubscribe at any time
1 Comment
  1. Steve says

    well explained

Comments are closed.