In this post we will learn common TestNG annotations including @Test, @BeforeMethod, @AfterMethod, @BeforeClass, @AfterClass, @BeforeGroups, @AfterGroups, @BeforeSuite, @AfterSuite, @BeforeTest & @AfterTest. Further posts next in tutorial explains in detail other popular annotations including @Parameters & @DataProvider annotations.
Previous post gave an introduction to TestNG. In this post, we will start using annotations in very simple, step-by-step approach. Let’s begin.
TestNG Annotations can be categorized based on there usage level(basic,advanced) and how frequently they are used.
1) Basic Annotations: Annotations which are most frequently used on day to day basis.
2) Advanced Annotations: Annotations which are used on special situations/setup/configurations.
Each annotation also comes with several attributes which can be used to alter the test execution behavior.
First thing first, let’s create a sample class whose methods will be unit-tested using testNG.
Below is the sample class we will use in this example.
Nothing special about this Calculator class, which provides methods for common arithmetic operations. We will unit test each of these methods.
//Ideal place to perform some setup which is shared among all tests.
//Ideal place to perform some cleanup of setup which is shared among all tests.
//May perform some initialization/setup before each test.
//May perform cleanup of initialization/setup after each test.
@BeforeClass: I run only once, before first test start.
@AfterClass: I run only once, after all tests have been done.
Method annotated with @BeforeClass will be executed only once, before the first test method in this class starts to run. It’s a good place to do any initialization or configuration setup which is common to all test methods and eventually may be used in those test methods. Note how we created an instance of calculator in @BeforeClass method which is used throughout all tests method. Method annotated with @AfterClass will be executed only once but only after all tests case have been executed. It provides a handle to do some cleanup after all tests in this class have been executed. You can see in above output that @BeforeClass and @AfterClass were executed only once and that too at very beginning and at very end.
Method annotated with @BeforeMethod will be executed before each test method. Similarly @AfterMethod method will be executed after each test method execution. @BeforeMethod annotated method is a good place to initialize some setup which can be used and eventually altered by the test method. Similarly @AfterMethod annotated method is a handy place to cleanup the setup created in @BeforeMethod and updated by @Test method itself.
Optionally, you can provide some specific arguments in @BeforeMethod and @AfterMethod methods in order to get some info related to the test in execution. In our example, we have declared an argument Method which eventually helps us to find the name of the test in execution.
Methods annotated with @Test are actual test methods, serving as unit test in above example. In @Test methods, we call real methods and assert the output/behavior from actual method for success/failure.
TestNG allows us to group several tests together. You can group certain tests based on what behavior/aspect they are actually testing. You may have a scenario where few tests belong to a certain group(say database) and other ones belong to other group(say security) and yet another one belong to other group(say UI). With this approach you may decide to execute only certain group of test and skip other ones(let’s say there was a regression on database related code, so only execute database related tests). Several Tests can belong to a group and a test can be part of several groups.
Post containing full example code for testNG grouping related aspects.
Every Test you run using TestNG runs as part of a suite. A Suite in testNG is represented by one XML file, usually named as testng.xml. <suite> tag is the first tag in XML file which represents a suite and it may contains one or more <test> tags. Each <test> tag in turn may contain one or more <classes>, <packages>, <groups> tags and more.
Post containing full example code for testNG suite related aspects.