在maven项目的pom.xml文件中导入TestNG
<?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>com.TestDemo.www</groupId>
<artifactId>TestDemo</artifactId>
<version>1.0.0</version> <dependencies>
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<version>6.14.3</version>
</dependency>
</dependencies> </project>
TestNG注解学习理解
package com.testng; import org.testng.annotations.*; public class BasicAnnotationStudy {
@Test
public void testcase1(){
System.out.println("测试用例1");
} @Test
public void testcase2(){
System.out.println("测试用例2");
} @BeforeMethod
public void BeforeMethod(){
System.out.println("BeforeMethod测试方法之前运行");
} @AfterMethod
public void AfterMethod(){
System.out.println("BeforeMethod测试方法之后运行");
} @AfterClass
public void AfterClass(){
System.out.println("AfterClass在类运行之前运行");
} @BeforeClass
public void BeforeClass(){
System.out.println("BeforeClass在类运行之后运行");
} @BeforeTest
public void BeforeTest(){
System.out.println("BeforeTest运行");
} @AfterTest
public void AfterTest(){
System.out.println("AfterTest运行");
} @BeforeSuite
public void BeforeSuite(){
System.out.println("BeforeSuite运行了");
} @AfterSuite
public void AfterSuite(){
System.out.println("AfterSuite运行了");
}
}
运行上面的Java文件, 得到如下结果, 比较直观的看出来每个注解的执行顺序, 如图
从testng.xml的内容上来看更容易理解上面的执行结果
<?xml version="1.0" encoding="utf-8" ?>
<suite name="your suite name">
<test name="your test name1">
<classes>
<class name="com.testng.BasicAnnotationStudy"/>
</classes>
</test>
<test name="your test name2">
<classes>
<class name="com.testng.BasicAnnotationStudy2"/>
</classes>
</test>
</suite>