Invocation count , Invocation time out and threadpoolsize concept in TestNG

TestNG provides a robust framework for testing with features that allow for fine-tuned control over how and when tests are run. Among these features, invocationCount, invocationTimeOut, and threadPoolSize play critical roles, especially when dealing with repetitive test execution, managing test execution time, and parallel execution. Let's explore each of these attributes in detail:

1. invocationCount

The invocationCount attribute is used to specify how many times TestNG should invoke a test method. It's particularly useful for repetitive testing or for simulating load by executing a method multiple times in succession.

Example Usage:

package invocationcount;

import java.time.Duration;

import org.openqa.selenium.By;

import org.openqa.selenium.WebDriver;

import org.openqa.selenium.chrome.ChromeDriver;

import org.testng.annotations.Test;

import io.github.bonigarcia.wdm.WebDriverManager;

public class Invocation

{

@Test(invocationCount = 5)

// This method will be executed 5 times

public void testMethod()

{

WebDriverManager.chromedriver().clearDriverCache().setup();

WebDriver driver = new ChromeDriver();

driver.get("https://randomuser.me/");

driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(10));

driver.manage().window().maximize();

} }

2. invocationTimeOut

The invocationTimeOut attribute sets the maximum amount of time (in milliseconds) that all invocations of the test method together should take. If the total time exceeds this threshold, TestNG will abort the test method's execution. This is especially useful for ensuring that a test method that's being run multiple times (via invocationCount) doesn't exceed a total allowable duration, preventing tests from hanging indefinitely.

Example Usage:

package invocationcount;

import java.time.Duration;

import org.openqa.selenium.By;

import org.openqa.selenium.WebDriver;

import org.openqa.selenium.chrome.ChromeDriver;

import org.testng.annotations.Test;

import io.github.bonigarcia.wdm.WebDriverManager;

public class Invocation

{

@Test(invocationCount = 5, invocationTimeOut = 5000)

// This method will be executed 5 times, but all invocations together must not take longer than 5 seconds.

public void testMethod2()

{

WebDriverManager.chromedriver().clearDriverCache().setup();

WebDriver driver = new ChromeDriver();

driver.get("https://randomuser.me/");

driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(10));

driver.manage().window().maximize();

} }

3. threadPoolSize

The threadPoolSize attribute is used in conjunction with invocationCount to run the specified number of invocations in parallel threads. This is particularly useful for concurrent execution, simulating multiple users or processes, and for reducing the total time taken for all invocations to complete.

Example Usage:

package invocationcount;

import java.time.Duration;

import org.openqa.selenium.By;

import org.openqa.selenium.WebDriver;

import org.openqa.selenium.chrome.ChromeDriver;

import org.testng.annotations.Test;

import io.github.bonigarcia.wdm.WebDriverManager;

public class Invocation

{

@Test(invocationCount = 10, threadPoolSize = 3)

// This method will be executed 10 times in parallel, with a maximum of 3 threads.

public void testMethod3()

{

WebDriverManager.chromedriver().clearDriverCache().setup();

WebDriver driver = new ChromeDriver();

driver.get("https://randomuser.me/");

driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(10));

driver.manage().window().maximize();

}

}

Conclusion

These attributes offer powerful mechanisms to enhance testing strategies, enabling more thorough testing scenarios such as stress, load, performance, and reliability testing. By understanding and correctly applying invocationCount, invocationTimeOut, and threadPoolSize, testers and developers can create more robust and reliable automated tests with TestNG.

To view or add a comment, sign in

More articles by mahenderkar sandeep

  • Boomi Developer

    Consulting and Digital Services Hyderabad Full Time 4 - 10 Years Experience Job Description: We are seeking a highly…

    1 Comment
  • Mulesoft Sr Developer

    Consulting and Digital Services Hyderabad Full Time 4 - 10 Years Experience Job Description: We are seeking a highly…

    6 Comments
  • Java Sr Developer

    Products and Platforms Hyderabad Full Time 4 - 10 Years Experience We are seeking an experienced Senior Java Developer…

  • Golang Sr Developer

    Consulting and Digital Services Hyderabad Full Time 4 - 8 Years Experience Senior Go (Golang) Developer Job…

  • Flutter Sr Developer

    Consulting and Digital Services Hyderabad Full Time 4 - 8 Years Experience Job Title: Senior Flutter Developer Job…

  • Lead QA

    Consulting and Digital Services Aiden AI India Full Time 8-14 Experience Job Summary: We are seeking a Testing Lead…

  • Senior Engineer - QA

    Senior Engineer - QA Consulting and Digital Services Aiden AI India Full Time 4-8 Experience Senior Software Engineer…

  • Interview questions with Deloitte round-2

    Date 30-07-2024 Duration : one hour 1. Self Introduction 2.

  • Selenium WebDriver Methods
  • Interview questions with Deloitte round-1

    Date 29-07-2024 Duration : one hour 1. Self Introduction 2.

    1 Comment

Insights from the community

Others also viewed

Explore topics