Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

How to group tests in Selenium using TestNG?

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 manage parameters using @Parameters annotation in Selenium?
  • Implement @DataProvider annotation to introduce data driven testing in Selenium
  • 200 Selenium Interview Questions
  • How to crack Vskills Selenium Professional Certification exam?

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

@Test(groups = {"group1"})

Multiple group name declaration

@Test(groups = {"group1", "group2"})

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.

package Test;

import org.testng.annotations.Test;

public class GroupsSampleProgram {
	
  @Test(groups = {"Sanity"})
  public void sanityTest1() {
	  System.out.println("This is sanity test 1");
  }
  
  @Test(groups = {"Regression"})
  public void regressionTest1() {
	  System.out.println("This is regression test 1");
  }
  
  @Test(groups = {"Sanity"})
  public void sanityTest2() {
	  System.out.println("This is sanity test 2");
  }
  
  @Test(groups = {"Regression"})
  public void regressionTest2() {
	  System.out.println("This is regression test 2");
  }
  
  @Test(groups = {"Sanity"})
  public void sanityTest3() {
	  System.out.println("This is sanity test 3");
  }
  
  @Test(groups = {"Regression"})
  public void regressionTest3() {
	  System.out.println("This is regression test 3");
  }
  
  @Test(groups = {"Sanity"})
  public void sanityTest4() {
	  System.out.println("This is sanity test 4");
  }
  
  @Test(groups = {"Regression"})
  public void regressionTest4() {
	  System.out.println("This is regression test 4");
  }
  
  @Test(groups = {"Sanity", "Regression"})
  public void regressionSanity() {
	  System.out.println("This is regression and Sanity both");
  }
}

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.

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 to run all the grouped tests. Here is the sample testng.xml file.

Console Output

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 inside the testng.xml file.

Console Output

 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.

The post How to group tests in Selenium using TestNG? appeared first on Inviul.



This post first appeared on Inviul, please read the originial post: here

Share the post

How to group tests in Selenium using TestNG?

×

Subscribe to Inviul

Get updates delivered right to your inbox!

Thank you for your subscription

×