TestNG Groups Test
By:Roy.LiuLast updated:2019-08-17
In this tutorial, we will show you how to do the group testing in TestNG.
1. Groups on Methods
Review a test group example.
- runSelenium() and runSelenium1() are belong to group selenium-test.
- testConnectOracle() and testConnectMsSQL() are belong to group database.
- runFinal() will be executed if groups selenium-test and database are passed.
TestGroup.java
package com.mkyong.testng.examples.group; import org.testng.annotations.AfterGroups; import org.testng.annotations.BeforeGroups; import org.testng.annotations.Test; public class TestGroup { @BeforeGroups("database") public void setupDB() { System.out.println("setupDB()"); @AfterGroups("database") public void cleanDB() { System.out.println("cleanDB()"); @Test(groups= "selenium-test") public void runSelenium() { System.out.println("runSelenium()"); @Test(groups= "selenium-test") public void runSelenium1() { System.out.println("runSelenium()1"); @Test(groups = "database") public void testConnectOracle() { System.out.println("testConnectOracle()"); @Test(groups = "database") public void testConnectMsSQL() { System.out.println("testConnectMsSQL"); @Test(dependsOnGroups = {"database","selenium-test"}) public void runFinal() { System.out.println("runFinal");
Output
//group = selenium-test runSelenium() runSelenium()1 //group = database setupDB() testConnectMsSQL testConnectOracle() cleanDB() //dependsOnGroups = database, selenium-test runFinal PASSED: runSelenium PASSED: runSelenium1 PASSED: testConnectMsSQL PASSED: testConnectOracle PASSED: runFinal
2. Groups on Class
The “Group” can be applied on class level. In below example, every public method of this class “TestSelenium” is belong to group selenium-test.
TestSelenium.java
package com.mkyong.testng.examples.group; import org.testng.annotations.Test; @Test(groups= "selenium-test") public class TestSelenium { public void runSelenium() { System.out.println("runSelenium()"); public void runSelenium1() { System.out.println("runSelenium()1");
Create an XML file to run 2 test classes.
testng.xml
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" > <suite name="TestAll"> <test name="final"> <classes> <class name="com.mkyong.testng.examples.group.TestSelenium" /> <class name="com.mkyong.testng.examples.group.TestGroup" /> </classes> </test> <!-- Run test method on group "selenium" only --> <test name="selenium"> <groups> <run> <include name="selenium-test" /> </run> </groups> <classes> <class name="com.mkyong.testng.examples.group.TestSelenium" /> <class name="com.mkyong.testng.examples.group.TestGroup" /> </classes> </test> </suite>
Output
//test name = final runSelenium() runSelenium()1 setupDB() testConnectMsSQL testConnectOracle() cleanDB() runFinal //test name = selenium runSelenium() runSelenium()1 =============================================== TestAll Total tests run: 7, Failures: 0, Skips: 0 ===============================================
3. Misc Examples
3.1 A test method can belong to multiple groups.
@Test(groups = {"mysql","database"}) public void testConnectMsSQL() { System.out.println("testConnectMsSQL");
3.2 The above result is executed via Eclipse TestNG Plugin.
References
From:一号门
Previous:TestNG + Spring Integration Example
COMMENTS