Annotations in TestNG

a couple of years ago

Annotations in Java programming language is a form of metadata (metadata is data or information which provides information about other data) that can be added to Java code.

Classes, methods, variables, parameters and packages can be annotated. Annotations have no direct effect on the operation of the code they annotate.

Annotations have a number of uses, among them:

  • Information for the compiler — Annotations can be used by the compiler to detect errors or suppress warnings.
  • Compile-time and deployment-time processing — Software tools can process annotation information to generate code, XML files, and so forth.
  • Runtime processing — Some annotations are available to be examined at runtime.

Annotations in TestNG can control the order of  execution. @Test is one annotation which makes a method a test case.

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.
  • @AfterClass – This method will be executed after all the methods of a class are invoked.
  • @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:

 

So, here first Method to be executed is one which has @BeforeSuite annotation, as the suite is at the top in the hierarchy. we will discuss the hierarchy in detail in testng.xml files.

Next method to be executed is one with @BeforeTest annotation. A test suite can have multiple or single test. This method will be executed before all the tests in a suite.

Next method to be executed is one with @BeforeClass annotation. This method will be executed before first method of all the classes included are executed.

Next method to be executed is one with @BeforeGroup and this method is executed before the first method of “Sanity” group as sanity group was passed as an attribute. (To understand this see Group execution article.)

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

All methods with annotation @After**** will be executed in reverse order.

Attributes which can be used with above annotations:

Attributes are some extra information we pass with annotations.

priority –

priority attribute controls the order of execution of a test case. Lower the value of integer higher is the priority.

enabled –

An attribute which takes boolean value and is used for enable/disable a test case.

alwaysRun –

An attribute when passed makes a test case execution mandatory.

For before methods (beforeSuite, beforeTest, beforeTestClass and beforeTestMethod, but not beforeGroups): If set to true, this configuration method will be run regardless of what groups it belongs to.

For after methods (afterSuite, afterClass, …): If set to true, this configuration method will be run even if one or more methods invoked previously failed or was skipped.

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).

Other Important Annotations:

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

We will learn these above annotations in upcoming tutorials with examples.

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

Saurabh Dhingra

About the Author

Saurabh Dhingra

Follow Saurabh Dhingra:

Leave a Comment: