TestNG test groups and it’s usage are explained in detailed in this post.
TestNG test groups Usage:
- Used for grouping of test methods
- These groups allows you to segregate test methods into sets and then runs different set of test methods back to back without recompiling the code.
- Specify group names in @Test annotation to make the test method as part of specific group.
- In the above code two types of test methods are available
- Sanity test methods should be run when doing the sanity test after the build is deployed to make sure that no basic functionality is broken.
- Regression test methods should be run to make sure all the scenarios are covered.
- In the above test method “sample1” executes when invoked with group “sanity”.
- Test methods “sample1” & “sample2” executes when invoked with group “regression”.
- We can specify any number of group names on test method. That means a method can belong to multiple groups like “sample1” above.
- Specify groups in testng.xml file <test> tag
- When invoked the above testng.xml file, it will run method “sample1” only.
Groups with regular expression
- Groups that are specified in testng.xml file supports regular expressions.
- To run specific tests on Firefox browser and specific tests in IE browser. Below is the example
- Following is the xml file to run Firefox test methods
- Here “firefox.*” means it includes test methods of groups “firefox.sanity” & “firefox.regression”.
- TestNG allows to exclude the groups.
- Specify <exclude> tag with group name in <groups> tags as below
- Sometimes we can exclude the test methods in the execution process using exclude strategy in groups. This might occur when some tests hae to be fixed and some tests are getting failed in the execution process.
- To do this, create a new group “broken” and then add to the methods to make these failed test methods belongs to it.
- For example in the above test method “sample2” is broken, so to disable it, we can do like this.
- As discussed above, test methods “sample1” & “sample2” belongs to “regression” group.
- But when executed the above xml file with exclude group, it will run only “sample1” test method.
Groups of Groups
- Groups can include other Groups as well, these groups are called Meta Group
- Following is the complete class that has test methods with groups, you can use directly by just copying it.
- You can define a group “all-sanity” that includes “sanity”, “firefox.sanity” and “ie.sanity”. Below is the complete xml file named as “groups.xml“.
- Below is the result when executed groups.xml file.
- It executes the test methods “sample1”, “testWithFirefox” & “testWithIE” as those are belonged to “sanity”, “firefox.sanity” & “ie.sanity” respectively.
In this post, we learned Test Groups, how to partition tests for execution, how to include and exclude groups & how to use groups with in other groups.