先简单介绍下注释
@BeforeClass(执行一次)
@BeforeMethod(N个Test 方法执行N次)
@Test Test方法(此注解可能在类上表示多个,在方法表示一个)
@AfterMethod(N个Test 方法执行N次)
@AfterClass(执行一次)
@BeforeSuite:在整个测试集运行之前运行;
@BeforeTest:在测试集中的每个Test之前运行。
区别:
@BeforeClass:在类运行之前执行
@AfterClass:在类运行之后执行
@BeforeMethod:在每个@Test方法执行前执行
@AfterMethod:在每个@Test方法执行后执行
1、配置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>org.example</groupId>
<artifactId>testng-oneday</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>org.hamcrest</groupId>
<artifactId>hamcrest-library</artifactId>
<version>1.2.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<version>7.0.0</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
2、配置完成后更新Maven配置
3、创建一个java Class
4、导入所需要的包
5、编写测试用例
import org.testng.annotations.*;
public class a823Test {
@BeforeClass
public void testBefor(){
System.out.println("test-one-beforClass");
}
@BeforeMethod(alwaysRun = true)
public void testMebefor(){
System.out.println("test-one-beforMethod");
}
@Test()
public void oneTest(){
System.out.println("test-one");
}
@AfterMethod(alwaysRun = true)
public void testMeafter(){
System.out.println("test-one-afterMethod");
}
@AfterClass
public void testAfter(){
System.out.println("test-one-afterClass");
}
}