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.

要查看或添加评论,请登录

mahenderkar sandeep的更多文章

  • Interview questions with Deloitte round-2

    Interview questions with Deloitte round-2

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

  • Selenium WebDriver Methods

    Selenium WebDriver Methods

  • Interview questions with Deloitte round-1

    Interview questions with Deloitte round-1

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

    1 条评论
  • Difference between Defect, Bug, Error and Failure

    Difference between Defect, Bug, Error and Failure

    What is a Defect? 1.The variation between the actual results and expected results is known as defect.

  • Levels of Testing

    Levels of Testing

    There are mainly four Levels of Testing in software testing : 1.Unit Testing : checks if software components are…

  • TestNG Annotations

    TestNG Annotations

    First, let's summarize the main annotations used in TestNG, which help in defining the execution order of the test…

    1 条评论
  • Parameterization concept in TestNG

    Parameterization concept in TestNG

    In software testing, particularly when using the TestNG framework for Java, parameterization is a powerful concept that…

  • Grouping concept in TestNG

    Grouping concept in TestNG

    In TestNG, the grouping concept allows you to categorize and organize test methods into logical groups based on certain…

  • Description annotation concept in TestNG

    Description annotation concept in TestNG

    In TestNG, the annotation is used to provide descriptive information about test methods or test classes. This…

  • Enabled Annotation concept in TestNG

    Enabled Annotation concept in TestNG

    In TestNG, there is no built-in annotation like in other testing frameworks such as JUnit 5. TestNG primarily relies on…

社区洞察

其他会员也浏览了