Grouping test cases in TestNG:
- We can group multiple test methods in a named group.
- You can easily classify your test cases based on requirement.
- You can perform a specific group of test methods related to a group or multiple groups.
- You can create a group of test methods based on the testing type, depending on modules or based on testing types like functional testing, sanity testing etc…
- Differentiate specific group test methods from all test methods.
Grouping Syntax:
1 | @Test(groups={"groupname"}) |
Grouping Example:
1 | @Test(groups={"sanity"}) |
Scenario’s:
- Before each and every method we use @Test(groups = { “sanity” }) method.
- Using groups method we provide multiple group names.
- As per group names execute methods from class.
Example: Grouping test cases in TestNG:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | package package_Name; import org.testng.annotations.Test; public class TestNG_Grouping { @Test(groups = { "sanity" }) public void methodOne() { System.out.println("Test method one belonging to sanity group."); } @Test(groups = { "smoke" }) public void methodTwo() { System.out.println("Test method two belonging to smoke group."); } @Test(groups = { "sanity" }) public void methodThree() { System.out.println("method three belonging to sanity group."); } } |
Running a TestNG group through testng.xml:
1. Execute single group using TestNG.xml file e.g. sanity
- If we want to execute only one group from class using TestNG.xml file.
- Then add group name into TestNG.xml file then run TestNG. xml file.
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 | <?xml version="1.0" encoding="UTF-8"?> <suite name="Sample Suite"> <test name="testing"> <groups> <run> <include name="sanity"/> </run> </groups> <classes> <class name="package_Name.TestNG_Grouping" /> </classes> </test> </suite> |
Note: Right click on testng.xml and run it as testng suite.
2. Execute Multiple groups using TestNG.xml file e.g. smoke / sanity
- If we want to execute Multiple group’s from class using TestNG.xml file.
- Then add Multiple group name’s into TestNG.xml file then run TestNG. xml file.
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 | <?xml version="1.0" encoding="UTF-8"?> <suite name="Sample Suite"> <test name="testing"> <groups> <run> <include name="sanity"/> <include name="smoke"/> </run> </groups> <classes> <class name="package_Name.TestNG_Grouping" /> </classes> </test> </suite> |
Note: Right click on testng.xml and run it as testng suite to execute / run Script.
How to skip tests using TestNG and TestNG Annotations, Groups & OnDepends Annotations