TestNG Annotations

Filed Under: TestNG

TestNG annotations helps us in quickly writing test cases, configure them programmatically. Let’s have a look at the most important TestNG annotations.

TestNG Annotations – Configuration

TestNG provides following annotations for configuring our test suite and test cases.

  1. @BeforeSuite: The method annotated will be executed before any test methods are executed.
  2. @AfterSuite: The annotated method will be run after all tests in this suite have run.
  3. @BeforeTest: The annotated method will be executed before any test classes in the suite is executed. It’s useful when we have a TestNG XML file, this method will run before any classes in test element.
  4. @AfterTest: The annotated method will be executed after all test classes in the suite is executed. It’s useful when we have a TestNG XML file, this method will run after all classes in test element.
  5. @BeforeGroups: The list of groups that this configuration method will run before. This method is guaranteed to run shortly before the first test method that belongs to any of these groups is invoked.
  6. @AfterGroups: The list of groups that this configuration method will run after. This method is guaranteed to run shortly after the last test method that belongs to any of these groups is invoked.
  7. @BeforeClass: The annotated method will be run before the first test method in the current class is invoked.
  8. @AfterClass: The annotated method will be run after all the test methods in the current class have been run.
  9. @BeforeMethod: The annotated method will be run before each test method.
  10. @AfterMethod: The annotated method will be run after each test method.

TestNG Most Important Annotations

Let’s look at the TestNG annotations that are mostly used and make it a powerful testing framework.

  1. @Test: The most important annotation that marks a class or method as part of the test. You can get more details about it at TestNG Tutorial.
  2. @DataProvider: This annotation marks a method as input data supplier for a test method. The annotated method must return an Object array that can be assigned as the parameters of the test method. The @Test method defines the data provider method name for supplying input data. You can read more about it at TestNG DataProvider.
  3. @Factory: This annotation is used to mark a method as a factory that returns objects to be used by TestNG as Test classes. The method must return Object[]. Read more at TestNG Factory.
  4. @Listeners: This annotation is used to define listeners on a test class. It’s a very powerful feature of TestNG framework, read more at TestNG Listeners.
  5. @Parameters: This annotation is used with TestNG XML suite to define the parameter name to be used from XML file for test method input arguments. You can read more about it at TestNG Parameters.

That’s all for a quick roundup on TestNG annotations.

You should check out TestNG annotations examples at our GitHub Repository.

Reference: TestNG Official Docs

Leave a Reply

Your email address will not be published. Required fields are marked *

close
Generic selectors
Exact matches only
Search in title
Search in content
Search in posts
Search in pages