In our last post on TestNG, we’d explained the three unique ways to install the TestNG plugin in Eclipse IDE. Now the next important item is to learn about the TestNG annotations.
Annotations are nothing but a piece of instruction for the compiler that you apply to classes, methods or variables in your Java code. It is a predominant feature of the TestNG framework.
There are multiple TestNG annotations which you can use for different tasks. It is essential to know each of the annotations so that you could apply them correctly while working on a TestNG Selenium Webdriver project.
Let’s see what are the TestNG annotations for Selenium Webdriver and what is their objective?
TestNG Annotations For Selenium WebDriver Project
We’ve already defined the generic concept of annotations in the first paragraph. And the TestNG annotations also work on the same lines and control the execution sequence of the test code and test methods.
We’ve drilled down the list of primary TestNG annotations along with a little detail about each of them.
List of TestNG Annotations
|@Test||Attaches a class or a method to become the part of the test.|
|@BeforeTest||Instructs the method to run before any test method related to the classes which are inside the <test> tag as per the <testng.xml> file.|
|@AfterTest||Halts a method from execution till all the test methods finish their execution. These methods belong to the classes defined in the <test> tag of <testng.xml> file.|
|@BeforeMethod||Allows a method to run before executing any of the @test annotated methods.|
|@AfterMethod||Allows a method to take off after all of the @test annotated methods finish their execution.|
|@Parameters||You can use this annotation for passing the parameters to the test methods.|
|@DataProvider||It marks a method as a data source for the test. Every @DataProvider annotated method must always return the value as <Object[ ][ ]>. You can use it in any of the @Test annotated methods.|
|@BeforeClass||The method annotated with @BeforeClass gets executed once before the first test method of the current class.|
|@AfterClass||The method annotated with @AfterClass gets run once after finishing all the test methods in the current class.|
|@BeforeGroups||It sets up the method to run before the first test method belonging to any of the groups involved in the execution.|
|@AfterGroups||It sets up the method to run after the execution of all the test methods belonging to any of the groups participating in the test.|
|@BeforeSuite||Any such method will get called before any of the suites runs from the test.|
|@AfterSuite||Any such method will stay its execution until all other methods in the current test suite get executed.|
|@Factory||You use it to execute any specific group of test cases with different values. It returns an array of test class objects as the <Object[ ]>.|
|@Listeners||You can use them with the test classes for the logging function.|
We believe the above list should be on your finger-tips. If you memorize it well, then it could come quite handy during Job interviews.
If you are planning for a testing Job interview, then you may like to check out the top 25 real-time questions on TestNG.
Must Read – TestNG interview questions
Advantages of using TestNG annotations.
There are many benefits of using the annotations in the project.
- Provides more delicate control over test execution
- Allows test case grouping and prioritization
- Enables parallelization of tests
- Support parameter testing
- Can connect to external data sources
- Have an excellent report-generating ability
- Strongly-typed means strong error checking
We hope you could use the above information in your TestNG projects.