Test case grouping

"Group" annotation is used to execute test cases in a groups. Let’s take an example, we have 100 tests of class vehicle and in it 10 methods of car, 10 methods of scooter and so on. With the Group anotation you can run all the scooter tests together in a batch and you can keep them all in a single test suite. 

Please see the following example illustrating Group annotation in more details.

1) Create two methods for Car, two methods for Scooter and one method in conjunction with Car & Sedan Car.

2) Group them separately with using  (groups = { ” Group Name” })

3) If we execute the group “Car” then it will execute the test methods which are defined with group as “Car”.

Example Code:
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
import org.testng.annotations.Test;

public class NewTest {

  @Test (groups = { "Car" })
  public void Car_01() {
   System.out.println("Testing car 1 in a batch");
  }

  @Test (groups = { "Car" })
  public void Car_02() {
   System.out.println("Testing car 2 in a batch");
  }

  @Test (groups = { "Scooter" })
  public void Scooter1() {
   System.out.println("Testing scooter 1 in a batch");
  }

  @Test (groups = { "Scooter" })
  public void Scooter2() {
   System.out.println("Testing scooter 2 in a batch");
  }

  @Test (groups = { "Car", "Sedan Car" })
  public void Sedan1() {
   System.out.println("Testing Sedan car 2 in a batch");
  }
}

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. Copy following content in testng.xml

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
<?xml version="1.0" encoding="UTF-8"?>
<suite name="Suite">
   <test name="Practice Grouping">
      <groups>
         <run>
            <include name="Car" />
         </run>
      </groups>
      <classes>
         <class name="Demo.Grouping" />
      </classes>
   </test>
</suite>

Run the test:
Run the test by right click on the testng.xml file and select Run As -> TestNG Suite.


Test case grouping


<-- Previous || Next ->

No comments:

Post a Comment