Let us understand What is TestNG?
TestNG stands for Testing Next Generation, and it is a very important framework that is inspired by Junit which provides us control over the execution of the test cases. TestNG is more powerful than Junit because it has some additional features to that of Junit features which makes it easier to write and run the tests.
Now, that we know what TestNG is let us understand what TestNG Groups are.
TestNG Groups allows us to perform grouping of different test methods to have control over the execution of different tests of different classes. It allows us to combine different groups in TestNG and include or exclude a set of groups of a class. Thus, it helps us to run test cases selectively by just grouping all the related test cases in our XML file. Example if we only want to run the smoke testing then we can define a group as “smoke” group and all the related test cases inside this “smoke” and run our test cases as per our requirement making it more streamlined and enable us to execute the multiple test cases parallelly.
Let’s start with an example to understand it better!
Step 1: Create few tests cases and define test methods using @Test annotation and groups attribute inside test annotation. In our example we will create class as "GroupTest" and define six test methods and divide into two groups “smoke” and “regression”.
So, now we have six test cases out of which three are “smoke” test cases and three are “regression” test cases as:
Groups | Test Cases | Test Cases | Test Cases |
---|---|---|---|
Smoke | OpenBrowser | AccountLogin | Cart |
Regression | WomenOutfit | MenOutfit | Electronics |
Step 2: Next step is to create a testing.xml file to define groups. It is also important to understand that TestNG groups are defined in testng.xml file with <groups> tag in <suite> tag or <test> tag.
If <groups> tag is applied within the <suite> tag, then it is applied to all the <test> tags of XML file.
If <groups> tag is applied within the <test> tag, then it is applied to that particular <test> tag only.
Refer below steps to create XML file:
Right click on your test class then click on TestNG at bottom and convert to TestNG. Click finish and then you will find testNG.xml file to left corner in your eclipse IDE
On clicking on testng.xml file we can define our group in this file to run our test cases. Now we will define our group inside <test>tag.
I have defined <groups> tag and also defined which groups to include and exclude. In our example we had two groups as “smoke” and “regression” which was created in our “GroupTest” class. we will include both the tests: smoke test and regression test. You can also try to exclude either group to have more understanding about groups. This is our final Tesng.xml file will look after making all the configuration.
Step 4: Final step is to run our test case. Right click on testing.xml file run as TestNG Suite. After that we can also create emailable report that makes it easier for a tester to share the report details like number of test cases skipped, failed, and passed, name of groups included and excluded etc. with other team members.
Below is the screen shot of emailable report.
We can conclude that TestNG Groups are very important as they provide us the feasibility to run the selective test cases as per our requirement and we can define this groups attribute in our testng.xml file with <groups> attribute and specify the groups which are to be included and excluded and run our test case to check our result output.
Comments