项目地址:https://gitee.com/neimenggudaxue/BasicTest
一、testNG注解
1.@BeforeSuite
被@BeforeSuite注解的方法,将会在testng定义的xml根元素里面的所有执行之前运行。
2.@AfterSuite
被@AfterSuite注解的方法,将会在testng定义的xml根元素里面的所有执行之后运行。
3.@BeforeTest
被@BeforeTest注解的方法,将会在一个元素定义的所有里面所有测试方法执行之前运行。
4.@AfterTest
被@AfterTest注解的方法,将会在一个元素定义的所有里面所有的测试方法执行之后运行。
5.@BeforeClass
被@BeforeClass注解的方法,将会在当前测试类的第一个测试方法执行之前运行。
6.@AfterClass
被@AfterClass注解的方法,将会在当前测试类的最后一个测试方法执行之后运行。
7.@BeforeMethod
被@BeforeMethod注解的方法,将会在当前测试类的每一个测试方法执行之前运行。
8.@AfterMethod
被@AfterMethod注解的方法,将会在当前测试类的每一个测试方法执行之后运行。
二、testNG.xml文件的执行
pom.xml文件
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>qufang</groupId>
<artifactId>test</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<version>6.11</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.30</version>
</dependency>
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<version>6.11</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<source>1.7</source>
<target>1.7</target>
<compilerArgs>
<arg>-Xlint:unchecked</arg>
<arg>-Xlint:deprecation </arg>
</compilerArgs>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.5</version>
<configuration>
<testFailureIgnore>true</testFailureIgnore>
<suiteXmlFiles>
<file>res/testNG.xml</file>
</suiteXmlFiles>
</configuration>
</plugin>
</plugins>
</build>
</project>
testNG.xml文件
<?xml version="1.0" encoding="utf-8" ?>
<suite name="Suite1" verbose="1">
<test name="test1">
<classes>
<class name="testNG.TestNGAnnotationTest" />
</classes>
</test>
</suite>
TestNGAnnotationTest类
package testNG;
import org.testng.annotations.*;
/**
* @Description: 接口:testng注解测试
* @Author: qufang
* @Date: Created in 下午5:44 2018/11/2
*/
public class TestNGAnnotationTest {
@BeforeSuite
public void beforeSuite(){
System.out.println(this.getClass().getName()+"BeforeSuite");
}
@BeforeTest
public void beforeTest(){
System.out.println(this.getClass().getName()+"BeforeTest");
}
@BeforeClass
public void beforeClass(){
System.out.println(this.getClass().getName()+"BeforeClass");
}
@BeforeMethod
public void beforeMethod(){
System.out.println(this.getClass().getName()+"BeforeMethod");
}
@AfterMethod
public void aftermethod(){
System.out.println(this.getClass().getName()+"AfterMethod");
}
@AfterClass
public void afterClass(){
System.out.println(this.getClass().getName()+"AfterClass");
}
@AfterTest
public void afterTest(){
System.out.println(this.getClass().getName()+"AfterTest");
}
@AfterSuite
public void afterSuit(){
System.out.println(this.getClass().getName()+"AfterSuite");
}
@Test
public void test1(){
System.out.println(this.getClass().getName()+" test1");
}
@Test
public void test2(){
System.out.println(this.getClass().getName()+" test2");
}
}
选中testNG.xml文件,点击run ,结果
testNG.TestNGAnnotationTestBeforeSuite
testNG.TestNGAnnotationTestBeforeTest
testNG.TestNGAnnotationTestBeforeClass
testNG.TestNGAnnotationTestBeforeMethod
testNG.TestNGAnnotationTest test1
testNG.TestNGAnnotationTestAfterMethod
testNG.TestNGAnnotationTestBeforeMethod
testNG.TestNGAnnotationTest test2
testNG.TestNGAnnotationTestAfterMethod
testNG.TestNGAnnotationTestAfterClass
testNG.TestNGAnnotationTestAfterTest
testNG.TestNGAnnotationTestAfterSuite
所以testNG.xml文件的执行实质就是依次执行冠以@Test注解的方法。而每执行一个@Test方法,都是依据<一>中的注解含义而形成的执行顺序
网友评论