This post demonstrates a simple example of using groups with TestNG.
For this exercise I’m using a very simple file layout. The jar file was copied directly from the TestNG package. SampleTest.java and testng.xml are my test and configuration files, respectively.
$ ls
SampleTest.java testng-5.10-jdk15.jar testng.xml
SampleTest.java has two test methods, annotated with @Test, and with an assigned group name.
public class SampleTest {
@Test(groups = { “groupA” })
public void methodA() {
System.out.println(“groupA”);
assert true;
}
@Test(groups = { “groupB” })
public void methodB() {
System.out.println(“groupB”);
assert true;
}
}
testng.xml defines these two groups as belonging to a test set.
I compile the SampleTest class,
and then run all the test methods as configured in testng.xml
[Parser] Running:
/Users/crashing/simpletestng/testng.xml
groupA
groupB
===============================================
TrialTest
Total tests run: 2, Failures: 0, Skips: 0
===============================================
Both test methods ran.
Alternatively I can specify specific groups to run. For this I use the -groups and -testclass option. I do not use the testng.xml file.
[Parser] Running:
Command line suite
groupB
===============================================
Command line suite
Total tests run: 1, Failures: 0, Skips: 0
===============================================
Specifying both the testng.xml and a -groups option will cause both configurations to be processed.
$ java -classpath .:testng-5.10-jdk15.jar org.testng.TestNG testng.xml -groups groupA -testclass SampleTest
[Parser] Running:
/Users/crashing/simpletestng/testng.xml
Command line suite
groupA
groupB
===============================================
TrialTest
Total tests run: 2, Failures: 0, Skips: 0
===============================================
groupA
===============================================
Command line suite
Total tests run: 1, Failures: 0, Skips: 0
===============================================
This is probably not what you want. So, use either the testng.xml or the -groups and -testclass option, not both.

No comments yet
Comments feed for this article