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.