How to use TestNG annotations and their execution flow
TestNG annotations are the most important part, they control the execution of test and other methods in the automation script. And in this post I am going to explain some of the important annoations. So, we will be covering following
In below example, there are two methods f and g with @test annotation and every other annotation has one method each.
Try to run this program on your setup and analyse the output. Below is the output.
[RemoteTestNG] detected TestNG version 6.14.3
f Test Case/Method
g Test Case/Method
Do you get the execution order. If not let me make it clear:
- First of all @BeforeClass is executed.
- Then @BeforeMethod
- @Test for f
- @Test for g
Now, you would be able to make out the execution flow. So, this way you can create your setup, teardown methods and use whatever before/after annotation with it as per your requirement. If you want to execute your setup only once at class level then use @BeforeClass or if you want to execute setup before every test case/method then use @BeforeMethod (Same for teardown).
Happy Learning! :)