概念
TestNg是开源的java自动化测试框架,设计灵感来源于Junit和Nunit。注解Annotation是从JDK1.5开始引入到java语言中,TestNg借鉴了Java注解来定义测试。
注解
@BeforeSuite
被@BeforeSuite注解的方法,将会在testng定义的xml根元素(suite标签)里面的所有执行前运行
@AfterSuite
被@AfterSuite注解的方法,将会在TestNg定义的xml根元素(suite标签)里面的所有执行之后运行
@BeforeTest
被@BeforeTest注解的方法,将会在一个元素定义(test标签)的所有里面所有的测试方法执行前运行
@AfterTest
被@AfterTest注解的方法,将会在一个元素定义(test标签)的所有里面所有的测试方法执行后运行
@BeforeClass
被@BeforeClass注解的方法,将会在当前测试类的第一个测试方法执行前运行
@AfterClass
被@AfterClass注解的方法,将会在当前测试类的最后一个测试方法执行之后运行
@BeforeMethod
被@BeforeMethod 注解的方法,将会在当前测试类的每一个测试方法执行前运行
@AfterMethod
被@AfterMethod注解的方法,将会在当前测试类的每一个测试方法执行后运行
XML配置
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="testNg测试">
<test name="Test1">
<classes>
<class name="TestAnnotation">
</class>
</classes>
</test>
</suite>
例子
例1
import org.testng.annotations.*;
import javax.sound.midi.SoundbankResource;
public class TestAnnotation {
@BeforeSuite
public void beforeSuite(){
System.out.println("this is beforeSuite");
}
@BeforeTest
public void beforeTest(){
System.out.println("this is beforeTest");
}
@BeforeClass
public void beforeClass(){
System.out.println("this is beforeClass");
}
@BeforeMethod
public void beforeMethod(){
System.out.println("this is beforeMethod");
}
@Test
public void test1(){
System.out.println("this is test1");
}
@Test
public void test2(){
System.out.println("this is test2");
}
@AfterMethod
public void afterMethod(){
System.out.println("this is afterMethod");
}
@AfterClass
public void afterClass(){
System.out.println("this is afterClass");
}
@AfterTest
public void afterTest(){
System.out.println("this is afterTest");
}
@AfterSuite
public void afterSuite(){
System.out.println("this is afterSuite");
}
}