TestNG Annotations


Annotations:

TestNG Framework in Selenium Webdriver having different annotations which will be very useful during Framework Development.

  1. @BeforeMethod
  2. @AfterMethod
  3. @Test
  4. @BeforeTest
  5. @AfterTest
  6. @BeforeClass
  7. @AfterClass
  8. @BeforeSuite
  9. @AfterSuite
Before getting into details about annotaions i will show the hierarchy for better understanding.




                              @BeforeSuite
                                  @BeforeTest
                                       @BeforeClass
                                          @BeforeMethod

                                               @Test

                                         @AfterMethod
                                      @AfterClass
                                  @AfterTest
                               @AfterSuite



1) @BeforeMethod:

       This method is executed before every testcase. suppose we are having 2 testcases then this       method will execute one time for Before testcase1 and another time for Before testcase2.


2) @AfterMethod:

     Same as Before method but ,This method is executed After every testcase. suppose we are having 2 testcases then this method will execute one time for After testcase1 and another time for After testcase2.


3) @BeforeClass:

         This Annotation is executed once per Before class.Suppose we are having 2  tescases in a class then this annotaion will be executed once Before Class.


4) @AfterClass:

         Same as Before class but,This Annotation is executed once per After class.Suppose we are having 2  tescases in a class then this annotaion will be executed once After Class.


5) @Test:

          This Annotation represents that this is the TestCase.


6) @BeforeTest: 

          This annotation is executed once Before all Testcase and class.suppose we are having 2 classes then this will execute once Before all the classes.


7) @AfterTest:

         This annotation is executed once After all Testcase and class.suppose we are having 2 classes then this will execute once After all the classes.


8) @BeforeSuite:

        This annotation is executed FIRST, of all annotations present in all classes.


9) @AfterSuite:

         This annotation is executed LAST, of all annotations present in all classes.



Example Program:




public class AnnotExample
{

@BeforeMethod
  public void m1()
  {
System.out.println("Before-Method");
   }


@Test
  public void Testcase1()
  {
System.out.println("TEST_CASE1");
  }


@Test
  public void TestCase2()
  {
System.out.println("Test-Case2");
  }


@AfterMethod
  public void m2()
  {
System.out.println("After-Method");
  }


@BeforeClass()
  public void m3()
  {
System.out.println("Before-Class");
  }


@AfterClass()
  public void m4()
  {
System.out.println("After-Class");
  }


@BeforeTest()
  public void m5()
  {
System.out.println("Before-Test");
  }


@AfterTest()
  public void m6()
  {
System.out.println("After-Test");
  }


@BeforeSuite
  public void m7()
  {
System.out.println("Before-Suite");
  }


@AfterSuite
  public void m8()
  {
System.out.println("After-Suite");
  }


}

OUTPUT:

Before-Suite

Before-Test
Before-Class
Before-Method
Test-Case2
After-Method
Before-Method
TEST_CASE1
After-Method
After-Class
After-Test
PASSED: TestCase2
PASSED: Testcase1

===============================================

    Default test
    Tests run: 2, Failures: 0, Skips: 0
===============================================

After-Suite


===============================================
Default suite

Total tests run: 2, Failures: 0, Skips: 0


No comments:

Post a Comment