How to group tests in Selenium using TestNG?

Category: Automation Framework, TestNG Tutorials 38 0

Whenever there is any in deployment or release we generally do sanity testing to make sure critical code is working fine as expected. Similarly, we also do regression testing after delivery of new functionality. In general, testers create a separate testng.xml file for each testing activity. This is not a wrong technique, but not an efficient and smart one. TestNG supports grouping of similar tests so that they could be called for the execution from a single testng.xml file. These group tests help in various ways to break down the complex test execution problems.

So, our agenda for this tutorial is to discuss how to group tests in our Selenium project with the help of TestNG file. The goal of delivery managers is to handover the defect-free product to the clients; hence testers play a crucial role to assure the quality of the product. This can only happen when testing processes are efficient and seamless.

Suggested Readings:

How to implement ‘group tests’ feature in the Selenium project using TestNG?

We have already discussed in previous tutorials that grouping tests are one of the properties and added advantage of using TestNG. So, the declaration of grouping is very simple. Here is the group declaration:

Single group name declaration

Multiple group name declaration

Run ‘Group tests’ declaration in testng.xml

These are the techniques to declare test groups.

Sample Program

Below is the sample program wherein we have grouped few tests as Smoke while few other as Regression and few as both, Smoke and Regression. We design our testng.xml file in such a way that our requirement to run specified groups get processed.

Here is the testng.xml file which includes all the groups.

We can define one group at a time.

Console output

Below screenshot is the console output generated after running above testng.xml file. It executes all the tests which are having groups=”Sanity” declaration.

One group tests

Let’s look at some other techniques to exclude and include tests to run.

How to include all tests based on group tests declaration?

Let’s look at the changes in testng.xml file which uses <define> </define> to run all the grouped tests. Here is the sample testng.xml file.

Console Output

Include Group Tests

As per the above screenshot from console we see that all the tests grouped with Sanity and Regression got executed.

How to exclude any test from the group tests?

This sample testng.xml file depicts exclusion of all the tests which contain their group’s definition as Regression. We use <exclude></exclude> inside the testng.xml file.

Console Output

Excluded group tests

 From the above console output, we observe that TestNG excluded all the tests which were having groups=”Regression” in it.

This was all about ‘group tests’ technique in Selenium using TestNG. Stay connected for more knowledge sharing session. Join our Facebook group for more updates.

Join Inviul fb group

Related Articles

Add Comment