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

TestNG annotations and easy examples

TestNG has become one of the most popular test automation tools in the market. This tool which was inspired in nUnit and jUnit, allows us to manage our test suites, reports and test configurations with Testng annotations, independently of the test automation framework we are using (It’s worth to mention that testng is based on Java, so the framework must be in Java as well).

I would like to show some examples about what we can do with this tools. As an old testng user I have used it for years on different projects on many different clients, so I will give the examples that based on my experience are the best approaches for testng.

F.A.Q
  • How to group test cases with testNG annotations?
  • How to send a parameter to a test case with testng?
  • How to prioritize test cases with testng?
  • How to use before and after testNG annotations?
  • How to add testNG to an automation project?
  • How to add testNG maven dependency?
  • How to use the @test testng annotation?
  • How to create a testng suite?
  • Using testng annotations

How to group test cases with testNG annotations?

One of the most common things we usually need to do with testng, is to group test cases into different categories so we can then run them by category. As an example, I use to group my automated test cases into at least this 3 main categories:

  • Regression
  • Smoke
  • Sanity

So to achieve this we can use the @groups tag to our test case:

@Test(groups = { "smoke" })
 public void test1() {
   System.out.println("test2");
 }

@Test(groups = { "regression" })
 public void test1() {
   System.out.println("test3");
 }

How to send a parameter to a test case with testng?

It’s always a good idea to parametrize our test cases, so we can read parameters from external files, databases, csv or json files. If we want to do this, we can use the @parameters annotation like this:

@Test(groups = { "regression" })
@parameters({"parametro_1"})
 public void test1(String parametro_1) {
   System.out.println("El parametro es " + parametro_1);
 }

How to prioritize test cases with testng?

If we want some test cases to be executed before others, we can use the @priority annotation, so those with higher priority will fun before lower priority ones. (The lower the number the higher the priority)

@Test(priority=1)
 public void test1() {
   System.out.println("Test 1");
 }

@Test(priority=2)
 public void test1() {
   System.out.println("Test 2");
 }

How to use before and after testNG annotations?

Before and after annotations allows us to execute any portion of code either before or after a test case, a test suite, or a group of tests.

For example, if we have the following code, with a before suite, an after suite and 2 test cases:

@beforeSuite
 public void test1() {
   System.out.println("Starting");
}

@afterSuite
 public void test1() {
   System.out.println("Ending");
 }

@Test(priority=1)
 public void test1() {
   System.out.println("Test 1");
 }

@Test(priority=2)
 public void test1() {
   System.out.println("Test 2");
 }

The execution order will be the following:

Starting
Test 1
Test 2
Ending

How to add testNG to an automation project?

Let’s dive into the details, and let’s Add Testng dependencies to a maven java project so we can start automating our tests right away.

How to add testNG maven dependency?

First thing we need to do is to add testng to our maven dependencies, so let’s take our pom file and add testng like this:

4.0.0com.elblogdesanti.selenium
    seleniumdemo
    jar1.0-SNAPSHOTseleniumdemohttp://maven.apache.org1.81.8UTF-8junit
            junit
            3.8.1org.seleniumhq.selenium
            selenium-java
            3.141.59org.testng
            testng
            6.8

Then we should execute the following command in our terminal in order to install all the dependencies and create our eclipse project:

mvn clean install && mvn eclipse:eclipse

How to use the @test testng annotation?

Now that we have testNG in our tests, we can just add the @test annotation to our test methods so testNG interprets them as actual tests cases it needs to execute:

package com.elblogdesanti.selenium;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.annotations.Test;

public class SeleniumTests {
    
    @Test
    public void myFirstTest()
    {
        WebDriver driver = new ChromeDriver();
        driver.get("http://elblogdesanti.com");
        driver.findElement(By.className("cs-icon-search")).click();
        driver.findElement(By.className("search-field")).sendKeys("la regla de los dos minutos");
        driver.findElement(By.className("search-submit")).click();
    }

}

How to create a testng suite?

TestNG test suites allows us to define suites, depending on many things like test classes, packages, annotations, etc. In the following example I will create a test suite that will execute all the tests inside the specified package:

If you want to create your own suite, just create a file (suite.xml) with the xml code from the above block, and change the class name so it matches your class name.

Using testng annotations

Now, let’s try to do something a little bit more complex. Until now, we had a single test case and a couple of annotations. Of course, in real life, we will have multiple test cases, so using best practices and good design patterns is a must if we want to have a test automation framework that scales.

In the following scenario we have 2 test cases with a beforeMethod and an AfterMethos annotation, so what will happen is that the code will be executed in the following order:

  • start()
  • myfirstTest()
  • end()
  • start()
  • mySecondTest()
  • end()

Or, in a different order, because we do not have a priority specified in our tests:

  • start()
  • mySecondTest()
  • end()
  • start()
  • myfirstTest()
  • end()
package com.elblogdesanti.selenium;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;

public class SeleniumTests {
    
    WebDriver driver;
    
    @BeforeMethod
    public void start() {
        driver = new ChromeDriver();
        driver.get("");
    }
    
    @AfterMethod
    public void end() {
        driver.close();
    }
    
    @Test
    public void myFirstTest()
    {
        driver.findElement(By.className("cs-icon-search")).click();
        driver.findElement(By.className("search-field")).sendKeys("la regla de los dos minutos");
        driver.findElement(By.className("search-submit")).click();
    }
    
    @Test
    public void mySecondTest()
    {
        driver.findElement(By.className("cs-icon-search")).click();
        driver.findElement(By.className("search-field")).sendKeys("la tecnica pomodoro");
        driver.findElement(By.className("search-submit")).click();
    }

}

As we can see, using testNG is pretty straightforward and the implementation will allow us to play with different configurations in our test executions.

If you want to download the samples, feel free to clone my GitHub repo with a working Selenium Webdriver project, you can use it as a base for your own experiments.

The post TestNG annotations and easy examples appeared first on Relevant Codes.

Share the post

TestNG annotations and easy examples

×

Subscribe to Relevant Codes — A Blog By Anshoo Arora Featuring Articles On Test Automation Tools, Frameworks And Code-design.

Get updates delivered right to your inbox!

Thank you for your subscription

×