When writing tests in TestNG, you will either mark your whole class with the @Test annotation (so that each public method that appears in your class will be considered a test method), or you will explicitly attach this annotation to every method you will run tests from. The latter approach allows for a bit of customization and differentiation among tests, by adding different values to the test attributes. Test attributes are @Test specific, and are to be specified right next to the @Test annotation. Some of the most common attributes are described below:

  • description: a string that gives information about the test it is attached to.
  • timeOut: this is the maximum number of milliseconds for a test run. When a test run exceeds the specified timeOut, it will throw an org.testng.internal.thread.ThreadTimeoutException: “Method org.testng.internal.TestNGMethod.methodName() didn’t finish within the time-out numberOfSeconds”. This is very useful when tests remain stuck, especially if you run them from a continuous delivery system, preventing the whole test suite that runs from being stuck indefinitely.
  • priority: when this attribute is not declared in your tests, the order in which the test methods are run when selecting to run all the tests in your class is random. For clarity, many times you would like to run some tests after others. That’s where ‘priority’ steps in. It can hold integer values between -5000 and 5000. The test with the lowest priority runs first, then the one with the next priority, and so on. For instance, if you have three tests, one having priority -5000, one with 0 and one with 10, the order in which they run is : -5000, 0, 10. The default priority, if none is specified, for all tests, is 0. Keep in mind that priority is different than dependency, hence the priority only specifies when to run a test, and tests are independent of each other.
  • dependsOnMethods: when you want to run a test only after another test has run successfully, making the second test’s run dependent on the first test’s successful outcome, you will specify this attribute. If the first test fails, the one dependent on it will not run.
  • enabled: this attribute has boolean values, and by default (when it is not specified), it is ‘true’. This attribute is only worth specifying explicitly when you don’t want a certain test method or class to be run, by setting the attribute to false. For example, if from your IDE you run the whole test class in which an ‘enabled=false’ method resides, this method will be skipped, while the others will be run.
  • groups: this attribute is useful for grouping together tests that relate to the same functionality, are of the same importance or are of the same type. You can use groups from within TestNG to either include tests having a particular group in a run, or to exclude them. To learn how to user this functionality, please refer to: https://imalittletester.com/2014/03/01/running-testng-tests/.

An example of a @Test method, that contains all the above attributes:

@Test(description = "This a new test", timeOut = 20000, priority = -200, dependsOnMethods = "test1", groups ="tutorial")
public void test2(){

The next step is to actually run these tests, which you can learn how to do here: https://imalittletester.com/2014/03/01/running-testng-tests/.

2 thoughts on “TestNG @Test attributes

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.