O'Reilly logo

TestNg Beginner's Guide by Varun Menon

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Before and After annotations

Before and After annotations are mainly used to execute a certain set of code before and after the execution of test methods. These are used to basically set up some variables or configuration before the start of a test execution and then to cleanup any of these things after the test execution ends.

TestNG provides five different kinds of Before and After annotation options, each of which can be used depending upon the test requirements. The following are the different before and after options provided by TestNG:

  • @BeforeSuite/@AfterSuite
  • @BeforeTest/@AfterTest
  • @BeforeGroups/@AfterGroups
  • @BeforeClass/@AfterClass
  • @BeforeMethod/@AfterMethod

Let's try out an example containing all the preceding annotated methods and learn about ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required