項(xiàng)目地址:https://gitee.com/neimenggudaxue/BasicTest
一、testNG注解
1.@BeforeSuite
被@BeforeSuite注解的方法,將會(huì)在testng定義的xml根元素里面的所有執(zhí)行之前運(yùn)行。
2.@AfterSuite
被@AfterSuite注解的方法,將會(huì)在testng定義的xml根元素里面的所有執(zhí)行之后運(yùn)行。
3.@BeforeTest
被@BeforeTest注解的方法,將會(huì)在一個(gè)元素定義的所有里面所有測試方法執(zhí)行之前運(yùn)行。
4.@AfterTest
被@AfterTest注解的方法,將會(huì)在一個(gè)元素定義的所有里面所有的測試方法執(zhí)行之后運(yùn)行。
5.@BeforeClass
被@BeforeClass注解的方法,將會(huì)在當(dāng)前測試類的第一個(gè)測試方法執(zhí)行之前運(yùn)行。
6.@AfterClass
被@AfterClass注解的方法,將會(huì)在當(dāng)前測試類的最后一個(gè)測試方法執(zhí)行之后運(yùn)行。
7.@BeforeMethod
被@BeforeMethod注解的方法,將會(huì)在當(dāng)前測試類的每一個(gè)測試方法執(zhí)行之前運(yùn)行。
8.@AfterMethod
被@AfterMethod注解的方法,將會(huì)在當(dāng)前測試類的每一個(gè)測試方法執(zhí)行之后運(yùn)行。
二、testNG.xml文件的執(zhí)行
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文件,點(diǎn)擊run ,結(jié)果
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文件的執(zhí)行實(shí)質(zhì)就是依次執(zhí)行冠以@Test注解的方法。而每執(zhí)行一個(gè)@Test方法,都是依據(jù)<一>中的注解含義而形成的執(zhí)行順序