Annotations in TestNG

August 5, 2016

Annotations are the keywords written before a method to provide a special meaning. say for example @Test is an annotation, the method written just after it is considered as a Testcase.

Annotations in TestNG can control the order of  execution. There are many @Before*** and @After*** annotations which can be executed before and after certain points respectively.

TestNG provides a wide range of annotations which makes this testing framework more flexible and provide better functionality as compared to JUnit.

Annotations in TestNG:

  • @Test – The method written after this annotation is considered as a test case.
  • @BeforeMethod – This method will be executed before every test case.
  • @AfterMethod – This method will be executed after every test case.
  • @BeforeClass – This method will be executed as a first method whenever a new class is loaded.
  • @BeforeGroup – This method will be executed before the first method of a group is executed.
  • @AfterGroup – This method will be executed after all the methods of a group are executed.
  • @BeforeTest – This method will be executed before all the methods of a test suite have run.
  • @AfterTest –  This method will be executed after all the methods of a test suite have run.
  • @BeforeSuite – This method will be executed before all the test cases of a test suite have run.
  • @AfterSuite – This method will be executed after all the test cases of a test suite have run.

Order of Execution of Annotations in TestNG:

After Executing the above code result is displayed as below:

annotations

 

So, here first Method to be executed is Before Suite, as the suite is at the top level in the hierarchy of test cases.

Next Method to be executed is Before Test. A Suite can have multiple tests as well. @BeforeTest will be executed before every test.

@BeforeTest will be executed before every test (Test run).

After BeforeTest method BeforeClass method is executed, as a new class is loaded.

Next method to be executed is BeforeGroup and this method is executed before the first method of “Sanity” group as sanity group was passed as an attribute.

After that, all the test cases are executed in their default order (alphabetic order).

Like this, all annotation with After will be executed in reverse order.

Few More Annotations:

  • @DataProvider
  • @Parameters
  • @Factory
  • @Listners

We will learn these annotations in upcoming tutorials with examples.

Attributes which can be used with @Test and other annotations:

  • priority – priority attribute controls the order of execution of a test case.
  • enabled – a boolean attribute which enables/disables a test case.
  • alwaysRun – an attribute when passed makes a test case execution mandatory.
  • dataProvider – to get test data from DataProvider defined in a class.
  • dependsOnMethod – A list of methods on which this test case depends on.
  • expectedExceptions – A list of exception which is expected is passed. If there is no exception or some different exception is coming, the test case will be marked as fail.
  • timeout – maximum time a test case will take to execute. (unit of timeout is milliseconds).

We will learn all these attributes mentioned above in upcoming tutorials.

PS: For any questions, suggestions or queries feel free to write us at saurabh@qatechhub.com or support@qatechhub.com. Happy Learning 🙂

Saurabh

About the Author

Saurabh

Follow Saurabh:

Leave a Comment: