Table of Contents
What we have learned so far?
Part 1 – What Is Testng? How To Configure Testng In Eclipse?
https://www.onlyfullstack.com/what-is-testng-how-to-configure-testng-in-eclipse/
https://www.onlyfullstack.com/testng-annotations/
https://www.onlyfullstack.com/how-to-run-test-suite-in-testng-with-surefire-plugin/
What is TestNG Group?
TestNG allows you to perform sophisticated groupings of test methods. Not only can you declare that methods belong to groups, but you can also specify groups that contain other groups. Then TestNG can be invoked and asked to include a certain set of groups (or regular expressions) while excluding another set. This gives you maximum flexibility in how you partition your tests and doesn’t require you to recompile anything if you want to run two different sets of tests back to back.
Groups are specified in your testng.xml file and can be found either under the or tag. Groups specified in the tag apply to all the tags underneath.
How to specify and run the TestNG group?
Lets create a functional-test group and write 2 test cases as below –
We can specify the group with groups parameter to the @Test, @BeforeGRoup and @AfterGroup.
package com.onlyfullstack.service; import org.testng.Assert; import org.testng.annotations.AfterClass; import org.testng.annotations.AfterGroups; import org.testng.annotations.AfterMethod; import org.testng.annotations.BeforeClass; import org.testng.annotations.BeforeGroups; import org.testng.annotations.BeforeMethod; import org.testng.annotations.Test; public class TestNGAnnotationsTest { @BeforeGroups(groups = {"functional-tests"}) public void beforeGroups() { System.out.println("@BeforeGroups"); } @AfterGroups(groups = {"functional-tests"}) public void afterGroups() { System.out.println("@AfterGroups"); } @BeforeClass public void beforeClass() { System.out.println("@BeforeClass"); } @AfterClass public void afterClass() { System.out.println("@AfterClass"); } @BeforeMethod public void beforeMethod() { System.out.println("@BeforeMethod"); } @AfterMethod public void afterMethod() { System.out.println("@AfterMethod"); } @Test(groups = {"functional-tests"}) public void runTest1() { System.out.println("@Test - runTest1"); } @Test(groups = {"functional-tests"}) public void runTest2() { System.out.println("@Test - runTest2"); } @Test() public void firstTest() { String input = "Employee"; Assert.assertEquals("EMPLOYEE", input.toUpperCase()); System.out.println("@Test - firstTest"); } }
Lete create separate testng-groups.xml and provide our group name to run from the classes specified in <classes> tag. Here we need to run only test cases which belongs to the functional-tests group.
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd"> <suite verbose="1" name="Onlyfullstack Suite" parallel="tests" thread-count="1"> <test name="Functional Tests"> <groups> <run> <include name="functional-tests"></include> </run> </groups> <classes> <class name="com.onlyfullstack.service.TestNGAnnotationsTest"/> </classes> </test> </suite>
Output –
@BeforeGroups @Test - runTest1 @Test - runTest2 @AfterGroups =============================================== Onlyfullstack Suite Total tests run: 2, Passes: 2, Failures: 0, Skips: 0 =============================================== Process finished with exit code 0
Source Code
You can find the source code on below link –
https://github.com/onlyfullstack/testng-tutorial
TestNG Tutorial
https://www.onlyfullstack.com/testng-tutorial-for-beginners/