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.
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.
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.
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.
dependsOnGroups
This attribute specifies group-level dependencies. The test method will run only after the specified group(s) have been executed successfully.
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.
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.
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.
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.
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.
invocationTimeout
Sets the maximum time (in milliseconds) for all invocations combined. Used with invocationCount.
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.
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.
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.
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.
threadPoolSize
Defines the number of threads to use when the test is invoked multiple times using invocationCount. It allows concurrent execution of test invocations.
Runs the test using up to 3 threads in parallel.
Note: This attribute is ignored if the invocation count is not specified.