Steps to Create Test Suite in TestNG

Step 1. Setting Up TestNG Environment

TestNG does not require any special kind of setup incase of IntelliJ Idea, both enterprise and community edition. Incase you choose to follow this tutorial on eclipse IDE, then you need to setup the TestNG environment before you can start using it in your application. To learn how to install and configure the plugin in the Eclipse IDE, please visit this page.

To Create a new TestNG project regardless of the IDE you choose to use, simply – Create a new Maven project and add the TestNG dependencies to it. Maven automatically provides us with a default project structure with a separate place to our unit tests in.

Step 2. Create a new Maven Project

To learn how to create a new maven project, visit this page.

Step 3. Add The following Dependencies to Your pom.xml file

XML
    <dependencies>
        <dependency>
            <groupId>org.seleniumhq.selenium</groupId>
            <artifactId>selenium-java</artifactId>
            <version>4.18.1</version>
        </dependency>
        <dependency>
            <groupId>org.seleniumhq.selenium</groupId>
            <artifactId>selenium-server</artifactId>
            <version>4.0.0-alpha-2</version>
        </dependency>
        <dependency>
            <groupId>org.testng</groupId>
            <artifactId>testng</artifactId>
            <version>7.9.0</version>
        </dependency>
        <dependency>
            <groupId>org.hamcrest</groupId>
            <artifactId>hamcrest-core</artifactId>
            <version>2.2</version>
        </dependency>
    </dependencies>


Step 4. Creating Test Classes

Explanation of test classes in TestNG:

  • Knowledge of TestNG is a basic pre-requisite of this tutorial. Please read this article for some background knowledge. Basically what we do is that we create our tests under the test directory that Maven created for us. Go to test > java > inverse domain name folder > package > classes.
  • Inverse Domain name means that suppose we have a domain name called w3wiki.org. The inverse domain name of this would be org.w3wiki. This is essential for TestNG to be able to locate your test classes.
  • Inside the folder with the inverse domain name that we have created, we can have as many packages we need. These test packages can be based on features or they may be based upon the different types of tests. This depends upon the specific needs of the project that you are working on and may be different for different companies.
  • And lastly, inside these test packages, we can write our test classes. These test classes allow us to write unit tests inside them. We can have as many test classes we need and as many tests inside them as we want.

Step 5. For this tutorial, we will create two test classes called TestClassA and TestClassB inside the java folder in the test directory.

TestClassA

Java
public class TestClassA {
    
    
}


TestClassB

Java
public class TestClassB {
       
}


Leave these classes as they are for now, we will right tests in them in the next section.

Step 6. Writing simple test methods with @Test annotation

  • Annotations like @Test in TestNG allow us to run our tests independently of the main class in Java. It reduces the need to write boilerplate code.
  • The @Test annotation is used to mark the unit tests in TestNG. They tell the TestNG environment that the following method is a unit test. Now we will add unit test to the classes we created earlier.

Step 7. Open the test classes you created earlier and add the unit tests to it.

TestClassA

Java
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;

import java.time.Duration;

public class TestClassA {
    private WebDriver driver;
    private WebElement searchBar;
    String searchBarXpath="//*[@id=\"RA-root\"]/div/div[1]/div[1]/div[2]/span/span/span[1]/input";
    String searchButtonXpath="//*[@id=\"RA-root\"]/div/div[1]/div[1]/div[2]/span/span/span[2]/button";
    
    @BeforeClass
    public void setUp() {
        driver=new ChromeDriver();
        driver.get("https://www.w3wiki.org/");
        driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(3));
        searchBar=driver.findElement(By.xpath(searchBarXpath));
    }

    @Test(groups = {"page-search"})
    public void testWebPageSearch() {
        searchBar.sendKeys("TestNG and Selenium");
        driver.findElement(By.xpath(searchButtonXpath)).click();
    }

    @AfterClass
    public void tearDown() {
        driver.quit();
    }
}


TestClassB

Java
import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;

import java.time.Duration;

public class TestClassB {
    private WebDriver driver;
    private WebElement searchIcon;
    private WebElement searchBar;
    String navSearchIconXpath="//*[@id=\"gcse-form\"]/button/i";
    String navSearchBarXpath="//*[@id=\"gcse-search-input\"]";

    @BeforeClass
    public void setUp() {
        driver=new ChromeDriver();
        driver.get("https://www.w3wiki.org/");
        driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(3));
        searchIcon= driver.findElement(By.xpath(navSearchIconXpath));
        searchIcon.click();
    }

    @Test(groups = {"nav-search"})
    public void testNavBarSearch() {
        searchBar=driver.findElement(By.xpath(navSearchBarXpath));
        searchBar.sendKeys("TestNG and Selenium");
        searchBar.sendKeys(Keys.ENTER);
    }

    @AfterClass
    public void tearDown() {
        driver.quit();
    }
}


Our Test Classes and unit tests are complete as of now. We will now proceed to organizing packages.

Step 8. Organizing test classes into packages

Create a folder with the inverse domain as com.w3wiki.browsertests. Move the test classes you created earlier inside this folder. This allows the test classes to be detected automatically by the testng environment. The final organization of files should match the following:

This completes the part concerning the test classes and unit tests for this tutorial. Now, we see how we can create test suites.

How to Create Test Suite in TestNG?

In this article we will learn about test suites and how can we build them using Java and TestNG. TestNG is a modern testing framework that is used very widely used today. It has a broad base of features that allow us to write unit tests and club them together in groups.

Table of Content

  • What is a Test Suite?
  • Steps to Create Test Suite in TestNG
  • Understanding TestNG XML Configuration
  • Creating Test Suites Using TestNG XML
  • Running Test Suites
  • Conclusion
  • FAQ’s on How to Create Test Suite in TestNG

TestNG is used in combination with Selenium to implement browser automation and therefore used for regression testing. By the end of this article, you will be successfully creating and running your own test suites.

Similar Reads

What is a Test Suite?

A test suite is a collection of test cases. There are various ways to create test these. You could either choose to group these tests by functionality or you could simply club your test classes together to form a test suite. In TestNG, the test suite is written in the form of a xml document. This allows you as a tester to have a fine grained control over what tests you want to run and create groups or suites according to the very specific requirements of your project....

Steps to Create Test Suite in TestNG

Step 1. Setting Up TestNG Environment...

Understanding TestNG XML Configuration

Introduction to TestNG XML configuration file...

Creating Test Suites Using TestNG XML

Guide to creating a basic test suite in TestNG XML...

Running Test Suites

Running Test Suites From IDE...

Conclusion

In this article we looked at what test suites are and how we can create them using TestNG in java. We looked at two approaches to clubbing tests, through groups and through classes. Hope you learnt something useful. If you want to discuss something, please feel free to leave comments below....

FAQ’s on How to Create Test Suite in TestNG

Where can I use the tag inside testng.xml?...