One of the nice features of TestNG is that it allows you to run specific groups of tests among all the tests that you have written. In my case, this feature is handy when I want to run read only tests on the production environment. The idea is to associate each test method to a group. When you want to run each group of tests separately, you only invoke your desired groups through the configuration file of TestNG. The following shows you how to do it.
To associate a test method to a group, simply specify the group name in the groups
attribute of the @Test
annotation.
@Test(groups={"readonly"}) public void myTest1(){ /*...*/ }
You can also associate a test to multiple groups by using commas as shown below:
@Test(groups={"readonly", "fast"}) public void myTest2(){ /*...*/ }
To choose which groups you would like to run, simply specify the name of the groups in the configuration file(testng.xml) of TestNG. In my case, I would like to run readonly
test methods. My configuration file looks like this:
<!DOCTYPE suite SYSTEM "http://beust.com/testng/testng-1.0.dtd" > <suite> <test> <groups> <run> <!-- List which groups to run.--> <include name="readonly"/> </run> </groups> <!-- Included all tests that you would like to run. --> <classes> <class name="my.classes.to.run" /> </classes> </test> </suite>
myTest1()
and myTest2()
will run.
Note: In the configuration file, besides defining which groups to run, you must include all the methods, classes or packages that you would like to run. Otherwise, no test will run if you are only specifying the groups and nothing else.