You are currently browsing the monthly archive for November 2009.
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.
<suite thread-count="5" verbose="1" name="TrialTest" annotations="JDK">
<test name="TestSet" enabled="true">
<groups>
<run>
<include name="groupA"/>
<include name="groupB"/>
</run>
</groups>
<classes>
<class name="SampleTest"/>
</classes>
</test>
</suite>
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.