TestNG Groups | TestNG Tutorial
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:
package softwareTestingMaterial; import org.testng.annotations.Test; public class TestCase1 { @Test (groups = { "smokeTest", "functionalTest" }) public void loginTest(){ System.out.println("Logged in successfully"); } }
Script – Test Case 2:
package softwareTestingMaterial; import org.testng.annotations.Test; public class TestCase2 { @Test (groups = { "functionalTest" }) public void composeMail(){ System.out.println("Mail Sent"); } }
testng.xml:
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" > <suite name="softwaretestingmaterial"> <test name="testngTest"> <groups> <run> <include name="smokeTest" /> </run> </groups> <classes> <class name="softwareTestingMaterial.TestCase1" /> <class name="softwareTestingMaterial.TestCase2" /> </classes> </test> </suite>
Console Output:
[TestNG] Running: Logged in successfully =============================================== softwaretestingmaterial Total tests run: 1, Failures: 0, Skips: 0 ===============================================
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:
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" > <suite name="softwaretestingmaterial"> <test name="testngTest"> <groups> <define name="all"> <include name="smokeTest"/> <include name="functionalTest"/> </define> <run> <include name="all" /> </run> </groups> <classes> <class name="softwareTestingMaterial.TestCase1" /> <class name="softwareTestingMaterial.TestCase2" /> </classes> </test> </suite>
Console Output:
[TestNG] Running: C:\Users\Administrator\Desktop\TestNGProject\testng.xml Logged in successfully Mail Sent =============================================== softwaretestingmaterial Total tests run: 2, Failures: 0, Skips: 0 ===============================================
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.
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" > <suite name="softwaretestingmaterial"> <test name="testngTest"> <groups> <run> <exclude name="smokeTest"/> <include name="functionalTest"/> </run> </groups> <classes> <class name="softwareTestingMaterial.TestCase1" /> <class name="softwareTestingMaterial.TestCase2" /> </classes> </test> </suite>
Console Output:
[TestNG] Running: Mail Sent =============================================== softwaretestingmaterial Total tests run: 1, Failures: 0, Skips: 0 ===============================================
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.
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.
Hello Sir,
Can you please make the font size more in Eclipse, i could’t able to see the text
Sure Rajini, we will try to do it in upcoming videos.
Hello Sir,
I am trying to revoke TestNG via testng.xml ….but after right click on “run as” my eclipse dont show the option of run as TestNG Suit. FYI, I created maven project and already have selenium and testng jars downloaded..
Hi Priyanka,
Try to create a new xml file.. follow the below xml file. Change package name and class names..