Monday, 17 November 2014

Testng Annotation Example programme

TestngAnnotation.java

import org.testng.annotations.Test;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeSuite;
import org.testng.annotations.AfterSuite;

public class TestngAnnotation {
// test case 1
@Test
public void testCase1() {
System.out.println(“in test case 1″);
}

// test case 2
@Test
public void testCase2() {
System.out.println(“in test case 2″);
}

@BeforeMethod
public void beforeMethod() {
System.out.println(“in beforeMethod”);
}

@AfterMethod
public void afterMethod() {
System.out.println(“in afterMethod”);
}

@BeforeClass
public void beforeClass() {
System.out.println(“in beforeClass”);
}

@AfterClass
public void afterClass() {
System.out.println(“in afterClass”);
}

@BeforeTest
public void beforeTest() {
System.out.println(“in beforeTest”);
}

@AfterTest
public void afterTest() {
System.out.println(“in afterTest”);
}

@BeforeSuite
public void beforeSuite() {
System.out.println(“in beforeSuite”);
}

@AfterSuite
public void afterSuite() {
System.out.println(“in afterSuite”);
}

}
/*
the output.
in beforeSuite
in beforeTest
in beforeClass
in beforeMethod
in test case 1
in afterMethod
in beforeMethod
in test case 2
in afterMethod
in afterClass
in afterTest
in afterSuite

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

  • First of all beforeSuite() method is executed only once.
  • Lastly, the afterSuite() method executes only once.
  • Even the methods beforeTest(), beforeClass(), afterClass() and afterTest() methods are executed only once.
  • beforeMethod() method executes for each test case but before executing the test case.
  • afterMethod() method executes for each test case but after the execution of test case.
  • In between beforeMethod() and afterMethod() each test case executes.*/

No comments:

Post a Comment