Run TestNG Tests on Real Devices

Seamlessly run TestNG with Selenium Tests on Real Devices and Browsers using BrowserStack Automate

Get Started free
Home Guide What is the use of @Test(invocationCount=x)?

What is the use of @Test(invocationCount=x)?

By Abishek Balakumar, Community Contributor -

When automating tests using TestNG, developers may face scenarios where they need to execute a test method multiple times.

TestNG’s @Test(invocationCount=x) annotation is a powerful tool for such situations. The annotation allows you to define how often a particular test method should be executed within the test suite.

This is particularly useful for reliability testing, performance testing, and even when a particular operation needs to be validated under different conditions multiple times.

What is @Test(invocationCount=x)?

The @Test(invocationCount=x) is an annotation in TestNG used to specify the number of times a test method should be executed. Instead of duplicating code and creating multiple instances of the same test method, you can simply use this annotation to define the repetition count.

For example,

If you need to run a method five times, using @Test(invocationCount=5) will ensure that the test method is executed exactly five times during the test execution.

This simplifies code maintenance and reduces redundancy, making it an efficient solution for developers dealing with flaky tests or stress testing requirements.

What is the use of @Test(invocationCount=x)? With Example

One of the primary uses of the @Test(invocationCount=x) annotation is to increase the reliability of tests.

Imagine you have a function that passes in one test run but fails intermittently. By running the test method multiple times, you can expose such hidden or transient bugs that might be overlooked with a single test run.

The annotation is also essential in performance testing scenarios.

For instance, if you want to measure your system’s performance or robustness when performing a particular operation multiple times, this annotation ensures the method is called repeatedly under the same conditions.

It is also useful for simulating repeated actions by end users, making it an ideal tool for automation testing.

BrowserStack Automate Banner

Example of @Test(invocationCount=x)

Here’s a basic example demonstrating how the @Test(invocationCount=x) works:

import org.testng.Assert;

import org.testng.annotations.Test;



public class InvocationCountExample {



    @Test(invocationCount = 5)

    public void testAddition() {

        Calculator calculator = new Calculator();

        int result = calculator.add(2, 3);

        Assert.assertEquals(result, 5, "Addition result is incorrect.");

    }

}

Explanation of @Test(invocationCount=x)

In the above example, the testAddition() method is executed five times. Each time, the add() method of the Calculator class adds two numbers (2 and 3), and the result is compared against the expected outcome (5) using Assert.assertEquals().

Output of @Test(invocationCount=x)

This method will run 5 times.
Test passed: All 5 invocations returned the expected result.

This repetitive execution ensures that the addition function is tested multiple times for consistency. TestNG will flag the test as a failure if any of the five invocations fail, helping identify intermittent bugs that might only occur in specific situations.

Selenium Example: Website Testing

Let’s see how @Test(invocationCount=x) can be used with Selenium to test a web application.

import org.openqa.selenium.WebDriver;

import org.openqa.selenium.chrome.ChromeDriver;

import org.testng.annotations.AfterClass;

import org.testng.annotations.BeforeClass;

import org.testng.annotations.Test;



public class SeleniumInvocationExample {

    WebDriver driver;

    @BeforeClass

    public void setup() {

        System.setProperty("webdriver.chrome.driver", "/path/to/chromedriver");

        driver = new ChromeDriver();

    }



    @Test(invocationCount = 3)

    public void testWebsiteTitle() {

        driver.get("https://example.com");

        String title = driver.getTitle();

        Assert.assertEquals(title, "Example Domain", "Title mismatch!");

    }

    @AfterClass

    public void teardown() {

        driver.quit();

    }

}

Explanation:

  • This example tests a website’s title three times. The testWebsiteTitle() method navigates to example.com and verifies the page title.
  • The repetitive execution ensures consistency in title validation across multiple runs.

Run Selenium Tests Using TestNG

Appium Example: Mobile Testing

Here’s how the @Test(invocationCount=x) annotation can be used with Appium to test a mobile application.

import io.appium.java_client.AppiumDriver;

import io.appium.java_client.MobileElement;

import io.appium.java_client.android.AndroidDriver;

import org.openqa.selenium.remote.DesiredCapabilities;

import org.testng.Assert;

import org.testng.annotations.BeforeClass;

import org.testng.annotations.Test;



import java.net.URL;



public class AppiumInvocationExample {



    AppiumDriver<MobileElement> driver;



    @BeforeClass

    public void setup() throws Exception {

        DesiredCapabilities caps = new DesiredCapabilities();

        caps.setCapability("deviceName", "Android Emulator");

        caps.setCapability("platformName", "Android");

        caps.setCapability("app", "/path/to/app.apk");



        driver = new AndroidDriver<>(new URL("http://localhost:4723/wd/hub"), caps);

    }



    @Test(invocationCount = 4)

    public void testAppLaunch() {

        MobileElement el = driver.findElementById("com.example.app:id/welcome_text");

        Assert.assertEquals(el.getText(), "Welcome", "Text mismatch on launch!");

    }

}

Explanation:

  • The testAppLaunch() method runs four times, validating that the app’s welcome screen displays the correct text each time it launches.
  • Repeating the test ensures the mobile app launches correctly and consistently across multiple runs.

Use Cases of @Test(invocationCount=x)

  • Reliability Testing: By running a test multiple times, you can validate whether your application consistently produces the same results across different invocations, helping to detect any transient bugs.
  • Performance Validation: You can stress test your application by invoking the same test repeatedly and measuring the time or resource consumption, which can highlight performance bottlenecks.
  • Regression Testing: When updating or modifying code, using @Test(invocationCount=x) allows regression tests to be performed multiple times to ensure no new issues are introduced.

Conclusion

The @Test(invocationCount=x) annotation is a highly efficient feature in TestNG that provides a simple mechanism to execute test methods multiple times without duplicating code.

If you are a tester or a developer, you can perform cross browser testing across multiple devices, browsers, and their versions with BrowserStack.

BrowserStack offers a real device cloud platform where you can access over 3500+ different device, browsers, and OS combinations using this platform, and it supports running Selenium tests using TestNG.

Talk to an Expert

Tags
Automation Frameworks Automation Testing Real Device Cloud Selenium

Featured Articles

How to Automate TestNG in Selenium

TestNG Annotations in Selenium Webdriver with Examples

Automation Tests on Real Devices & Browsers

Seamlessly Run Automation Tests on 3500+ real Devices & Browsers