Join 500+ Software Testing professionals. Solve problem and get your problem solved. Register Now!

0
how do i execute tests in group using TestNG?

Solved 1 Answers 79 Views Programming
0 1
how do i execute tests in group using TestNG?

1 Answer

0
Best answer
0
There are two ways to group TestNG classes
 
1. Groups on Methods
 
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
 
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
===============================================
 
answered by
selected by

Your answer

Your name to display (optional):
Privacy: Your email address will only be used for sending these notifications.
Anti-spam verification:
To avoid this verification in future, please log in or register.
...