TestNG Attributes

In TestNG, attributes are additional properties that can be used inside annotations like @Test, @BeforeMethod, etc., to control the behavior of the method. They make TestNG highly configurable by enabling custom behavior such as setting priorities, grouping tests, managing timeouts, handling dependencies, and more.

Commonly Used @Test Attributes

alwaysRun

Runs the test method regardless of dependent method has failed. 

@Test
public void login() {
    System.out.println("Login method ..");
    Assert.fail("Login failed");
}
@Test(dependsOnMethods = "login", alwaysRun = true)
public void registration() {
    System.out.println("Registration method ..");
}


Note: If alwaysRun = false, registration() would be skipped. With true, it runs even if login() fails.

dependsOnMethod

Specifies method dependencies. The test runs only after the specified method(s) pass. 

@Test
public void login() {
    System.out.println("Login method ..");
    Assert.fail("Login failed");
}
@Test(dependsOnMethods = "login", alwaysRun = false)
public void registration() {
    System.out.println("Registration method ..");
}


Note: registration() will be skipped because login() failed and alwaysRun is false.

dataProvider

This attribute specifies the name of the data provider method for a test. It is useful for running the same test with different sets of input data. This approach is known as data-driven testing, and dataProvider enables it by supplying dynamic inputs at runtime.

@DataProvider(name = "TestData")
public Object[][] data() {
    return new Object[][] { {"admin", "admin123"}, {"user", "pass"} };
}
@Test(dataProvider = "TestData")
public void loginTest(String username, String password) {
    System.out.println("Login with: " + username + " / " + password);
}


dataProviderClass

This attribute allows specifying a separate class that contains the data provider method. It helps organize test logic and test data in different classes, which improves code maintainability.

@Test(dataProvider = "clientData", dataProviderClass = ClientDetailsTest.class)
public void clientTest(String name) {
    System.out.println("Client: " + name);
}


dependsOnGroups

This attribute specifies group-level dependencies. The test method will run only after the specified group(s) have been executed successfully.

@Test(groups = {"smoke"})
public void accountSummary() {
    System.out.println("Account Summary");
}
@Test(dependsOnGroups = {"smoke"})
public void logout() {
    System.out.println("Logout");
}


description

This attribute provides a textual description for the test method. It helps document the test case’s purpose or any additional information needed for reference.

@Test(description = "Regression Test Summary")
public void regressionTest() {
    System.out.println("Running regression test...");
}


Enabled

This attribute helps determine whether or not to run a specific test method in the current suite or class. If set to false, the test is skipped during execution.

@Test(enabled = false)
public void skippedTest() {
    System.out.println("This won't run");
}
@Test(enabled = true) 
public void enabledTest(){
    System.out.println("This will run");
}


expectedExceptions

Defines the exception(s) expected to be thrown by the test method. The test is marked as passed only if the specified exception occurs during execution. If no exception or a different one is thrown, the test fails.

@Test(expectedExceptions = ArithmeticException.class)
public void divideByZero() {
    int result = 1 / 0;
}


group

This attribute allows assigning a test method to one or more groups. Grouping tests helps to organize and selectively run test suites like smoke, regression, etc.

@Test(groups = {"Regression"})
public void runRegressionTest() {
    System.out.println("Regression Test");
}


invocationCount

This attribute is used to specify the number of times a test method should be executed. It is useful for running the same test repeatedly to validate stability or behavior under load.

@Test(invocationCount = 3)
public void login() {
    System.out.println("Login test");
}


invocationTimeout

Sets the maximum time (in milliseconds) for all invocations combined. Used with invocationCount.

@Test(invocationCount = 3, invocationTimeOut = 3000)
public void registration() {
    System.out.println("Registration method ..");
}


priority

This attribute controls the execution order of test methods. By default, TestNG assigns priority 0 to all tests and executes them in ascending order.

@Test(priority = 0)
public void logout() {
    System.out.println("this executes first");
}
@Test(priority = 1)
public void logout() {
    System.out.println("this executes second");
}
@Test(priority = 2)
public void logout() {
    System.out.println("this executes thrid");
}


successPercentage

This attribute defines the minimum pass rate (as a percentage) for a test that runs multiple times using invocationCount. If the success percentage is not met, the test is marked as failed.

@Test(invocationCount = 5, successPercentage = 80)
public void flakyTest() {
    // Some logic that might fail occasionally
}


This test passes if at least 4 out of 5 runs pass.

singleThreaded

If set to true, all the test methods in the class will run in the same thread, even if TestNG is configured to run tests in parallel. This is applicable only at the class level.

@Test(singleThreaded = true)
public class MyTests {
    // all tests inside this class will run in the same thread
}


Use this at the class level with @Test(singleThreaded = true) on the class declaration.

timeOut

This attribute sets a maximum execution time (in milliseconds) for a test method. If the test takes longer than the specified time, it is marked as failed.

@Test(timeOut = 1000)
public void logout() {
    System.out.println("Logout");
}


threadPoolSize

Defines the number of threads to use when the test is invoked multiple times using invocationCount. It allows concurrent execution of test invocations.

@Test(invocationCount = 5, threadPoolSize = 3)
public void loadTest() {
    System.out.println("Running in thread: " + Thread.currentThread().getId());
}


Runs the test using up to 3 threads in parallel.
Note: This attribute is ignored if the invocation count is not specified.
 

Related Tutorials