How To Use Annotations in TestNG

Posted By : Manish Kumar | 23-Apr-2018

A considerable lot of us get confused about the pattern of running test methods while creating a test script, it's the most important to run test cases in correct order, and for that either we need to create them in one by one order in a series or make them in different classes and call them one by one which is again a different problem.

 
Also if we want to run test cases parallelly then it is not conceivable.
 
The solution to all these troubles is resolved by the TestNG annotations, by using these we can simply define the order of execution, also some additional parameters can be passed with the annotations if required.
 
The below listed are some annotations which are used:
 
@BeforeSuite: All such methods which are defined with this annotations will be executed before the first declared method in the suite.
@AfterSuite: Method with such annotation will be executed after all the test cases in the suite are executed.
@BeforeTest: This annotation makes the method to run before any test method that is declared in the test run
@AfterTest: The annotated method will run after all the test cases in the test are executed.
@BeforeGroups: The annotated method will run just before any of the method which is defined in the group.
@BeforeClass: This annotation is used to run a method before any method of the class is executed
@AfterClass: This annotation is used when a method is needed to run after all the methods in the class.
@BeforeMethod: The method with this annotation will run before each test method that is queued in the execution
@AfterMethod:  The method with this annotation will run after each test method that is queued in the execution
 
Advantages of TestNG annotations:
  1. It gives a liberty of grouping and prioritizing the test cases without any alteration in our code.
  2. We simply need to write our business logic and insert TestNG annotations as per our requirement of execution.
  3. And rest all will be tackled by the TestNG.

 
Thanks,
Related Tags

About Author

Author Image
Manish Kumar

Manish is Adaptive and passionate to learn new technical skills, have knowledge of testing and love to play badminton in free time

Request for Proposal

Name is required

Comment is required

Sending message..