For the fundamentals of testNG framework and the testng.xml content, please refer to my prev blog post.
- selenium-testng (See all the available annotations)
- selenium testNGxml
Let’s jump into a sample code to get more understanding on the annotations-
These below 2 classes contains all the details of testNG annotations and the output shows sequence of execution of each annotation.
Below examples also shows difference between all available testNG annotations, we will be discussing about each one once we get the output [towards end of this post]
1st Class: testcase1 – with @BeforeSuite, @AfterSuite. @Test, @AfterMethod and @BeforeMethod
package annotationpkg; import org.testng.annotations.*; public class testcase1 { @BeforeSuite public void setup() { System.out.println("BeforeSuite:Initiate database connection"); } @BeforeTest public void openurl() { System.out.println("BeforeTest:Open the AUT URL"); } @Test public void tc01() { System.out.println("Test:testcase logic"); } @Test public void tc_report() { System.out.println("Test:testcase report logic"); } @AfterTest public void closeurl() { System.out.println("AfterTest:close the AUT URL"); } @AfterSuite public void closeAll() { System.out.println("AfterSuite:close database connection"); } }
2nd class: testcase2 – with @Test, @AfterMethod and @BeforeMethod
package annotationpkg; import org.testng.annotations.*; public class testcase2 { @BeforeMethod public void openurl() { System.out.println("BeforeMethod:Open the AUT URL"); } @Test public void tc02() { System.out.println("Test:testcase logic"); } @Test public void tc03() { System.out.println("Test:testcase logic"); } @AfterMethod public void closeurl() { System.out.println("AfterMethod:close the AUT URL"); } }
Let’s see the xml for the above classes – Name it as testng_annotation.xml
<!DOCTYPE suite SYSTEM "https://testng.org/testng-1.0.dtd" > <suite name="MySuite" parallel="none"> <test name="MyTest1"> <classes> <class name="testNGSample.TC1"/> </classes> </test> <test name="MyTest2"> <classes> <class name="testNGSample.TC2"/> </classes> </test> </suite>
Let’s run the above testng_annotation.xml
and watch the output
BeforeSuite:Initiate database connection BeforeTest:Open the AUT URL Test:testcase logic Test:testcase report logic AfterTest:close the AUT URL BeforeMethod:Open the AUT URL Test:testcase logic AfterMethod:close the AUT URL BeforeMethod:Open the AUT URL Test:testcase logic AfterMethod:close the AUT URL AfterSuite:close database connection
Explanation
As you can see, we have mentioned @BeforeSuite
and @AfterSuite
only once irrespective of how many java classes are in a package or project, these annotations will be executed only once, so no need to mention in both the classes.
In testcase1, @BeforeTest
and @AfterTest
willl be executed only once before and after all the methods run (as mentioned under the <Test> tag in testng_annotation.xml)
Whereas in testcase2, @BeforeMethod
& @AfterMethod
will be executed each time a @Test method runs, we have 2 methods tc02 and tc03, so @BeforeMethod & @AfterMethod executed twice.
Next, let’s understand about testNG parameterization