TestNG Groups:
TestNG allows you to perform sophisticated groupings of test methods. Not only can you declare that methods belong to groups, but you can also specify groups that contain other groups. Then TestNG can be invoked and asked to include a certain set of groups (or regular expressions) while excluding another set. This gives you maximum flexibility in how you partition your tests and doesn’t require you to recompile anything if you want to run two different sets of tests back to back.
Groups are specified in your testng.xml file and can be found either under the <test> or <suite> tag. Groups specified in the <suite> tag apply to all the <test> tags underneath.
Here is a video tutorial to learn “TestNG Groups”:
Please be patient. The video “TestNG Groups” will load in some time.
If you liked this video, then please subscribe to our YouTube Channel for more video tutorials.
Script – Test Case 1:
2 4 6 8 | importorg.testng.annotations.Test; @Test(groups={'smokeTest','functionalTest'}) System.out.println('Logged in successfully'); } |
Script – Test Case 2:
2 4 6 8 | import org.testng.annotations.Test; @Test(groups={'functionalTest'}) System.out.println('Mail Sent'); } |
testng.xml:
2 4 6 8 10 12 14 16 18 | <!DOCTYPE suite SYSTEM'http://testng.org/testng-1.0.dtd'> <suite name='softwaretestingmaterial'> <run> </run> <classname='softwareTestingMaterial.TestCase1'/> </classes> </suite> |
Console Output:
2 4 6 8 | softwaretestingmaterial |
Group of Groups in TestNG Groups:
Groups can also include other groups. These groups are called MetaGroups. For example, you might want to define a group all that includes smokeTest and functionalTest. Let’s modify our testng.xml file as follows:
testng.xml – Group of Groups:
2 4 6 8 10 12 14 16 18 20 | <!DOCTYPE suite SYSTEM'http://testng.org/testng-1.0.dtd'> <suite name='softwaretestingmaterial'> <groups> <include name='smokeTest'/> </define> <include name='all'/> </groups> <classname='softwareTestingMaterial.TestCase1'/> </classes> </suite> |
Console Output:
2 4 6 8 10 | C:UsersAdministratorDesktopTestNGProjecttestng.xml Logged insuccessfully softwaretestingmaterial |
Groups Exclusion:
TestNG allows you to include groups as well as exclude them. You can ignore a group by using the <exclude> tag as shown below:
Here is a video tutorial to learn “Exclusion of Groups in TestNG”:
Please be patient. The video will load in some time.
If you liked this video, then please subscribe to our YouTube Channel for more video tutorials.
For example, it is quite usual to have tests that temporarily break because of a recent change, and you don’t have time to fix the breakage yet. However, you do want to have clean runs of your functional tests, so you need to deactivate these tests but keep in mind they will need to be reactivated. Project reality fallout new vegas 4.
2 4 6 8 10 12 14 16 18 20 | <!DOCTYPE suite SYSTEM'http://testng.org/testng-1.0.dtd'> <suite name='softwaretestingmaterial'> <exclude name='smokeTest'/> </run> </groups> <classes> <classname='softwareTestingMaterial.TestCase2'/> </test> |
Console Output:
2 4 6 8 | softwaretestingmaterial |
You can also disable tests on an individual basis by using the “enabled” property available on both @Test and @Before/After annotations.
Check this post – How to Ignore TestNG Tests.
You could find the complete TestNG tutorial here.
Album art finder. If you are not regular reader of my blog then I highly recommend you to signup for the free email newsletter using the below link.
SUBSCRIBE TO GET FREE EBOOK AND REGULAR UPDATES ON SOFTWARE TESTING
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”.
Groups Exclusion
- 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.
Summary:
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.
Goal: To Run TestNG using Command Prompt:
Steps to achieve our goal – Executing TestNG using Command Prompt:
- Open Eclipse and create a Java class
- Write a Java program
- Convert the Java Program into TestNG
- Open command prompt
- Run the TestNG using command prompt
Here is a video tutorial to learn “How To Run TestNG Using Command Prompt”:
Please be patient. The video will load in some time.
Step i: Open Eclipse and create a Java class
Testng Run Test Classes In Order
Step ii. Keep all the library files in a folder (here I create a folder name “lib”)
Step iii: Write a Java program
Step iv: Convert the Java Program into TestNG
Step v: Open command prompt
Step vi: Run the TestNG using command prompt