Run specific groups of tests

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.

  1. To associate a test to a group, simply specify the group name in the groups attribute of the @Test annotation.
    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(){ /*...*/ }
  2. 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 "" >
            <!-- List which groups to run from all the tests included.-->
            <include name="readonly"/>
        <!-- Included all tests that you would like to run. -->
          <class name="" />
    myTest1() and myTest2() will run. Note: In the configuration file, besides defining which groups to run, you must include all the methods/classes/packages that you would like to run. Otherwise, no test will be run if you are only specifying the groups and nothing else.