For the fundamentals of testNG framework and the testng.xml content, please refer to my prev blog post.
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 @AfterTest and @AfterMethod
1st Class: testcase1
Another class: testcase2 with @Test, @AfterMethod and @BeforeMethod
As you can see in above 2 classes, in only one class, I have mentioned @Before and @AfterSuite, as this will be execute only once, so no need to mention in both the classes.
Let's see the xml for the above classes - Name it as testng_annotation.xml
In testcase1, @BeforeTest and @AfterTest willl be executed only once before and after all the methods run (as mentioned under the 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.
Let's get into bit more depth about testNG, refer testNG parameterization
- selenium-testng (See all the available 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 @AfterTest and @AfterMethod
1st Class: testcase1
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 | 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"); } } |
Another class: testcase2 with @Test, @AfterMethod and @BeforeMethod
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | 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"); } } |
As you can see in above 2 classes, in only one class, I have mentioned @Before and @AfterSuite, as this will be execute only once, so no need to mention in both the classes.
Let's see the xml for the above classes - Name it as testng_annotation.xml
xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="MySuite" parallel="none">
<test name="MyTest1">
<classes>
<class name="annotationpkg.testcase1"/>
</classes>
</test>
<test name="MyTest2">
<classes>
<class name="annotationpkg.testcase2"/>
</classes>
</test>
</suite>
Let's run the above testng_annotation.xml and watch the outputIn testcase1, @BeforeTest and @AfterTest willl be executed only once before and after all the methods run (as mentioned under the
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.
Let's get into bit more depth about testNG, refer testNG parameterization
No comments:
Post a Comment