1.创建一个maven工程
创建好的工程目录如下所示:
2.创建一个子工程module(创建时需要将pom文件的<packaging>jar</packaging>改为pom)
文件结构如下所示:
此时maven工程在报错,则选择maven工程右键update project
在pom中引入testng的jar包
<dependencies>
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<version>6.9.8</version>
<scope>test</scope>
</dependency>
</dependencies>
3.创建一个testng的类
建好之后,目录结构如下所示:
编写第一个测试代码:
代码如下:
package testng20210715; import org.testng.Assert; import org.testng.annotations.AfterClass; import org.testng.annotations.AfterMethod; import org.testng.annotations.AfterSuite; import org.testng.annotations.AfterTest; import org.testng.annotations.BeforeClass; import org.testng.annotations.BeforeMethod; import org.testng.annotations.BeforeSuite; import org.testng.annotations.BeforeTest; import org.testng.annotations.Test; public class firstTestng { @BeforeSuite public void beforeSuite() { System.out.println("beforeSuite......."); } @BeforeMethod public void beforeMethod() { System.out.println("beforeMethod......."); } @BeforeClass public void beforeClass() { System.out.println("beforeClass......."); } @BeforeTest public void beforeTest() { System.out.println("beforeTest......."); } @Test public void test1() { System.out.println("这是第一个测试......"); Assert.assertEquals(add(1,1), 2); } @Test public void test2() { System.out.println("这是第二个测试......"); } @AfterTest public void afterTest() { System.out.println("afterTest......"); } @AfterClass public void afterClass() { System.out.println("afterClass......"); } @AfterMethod public void afterMethod() { System.out.println("afterMethod......"); } @AfterSuite public void afterSuite() { System.out.println("afterSuite......"); } public int add(int i,int j){ return i+j; } }
运行结果如下:
beforeSuite.......
beforeTest.......
beforeClass.......
beforeMethod.......
这是第一个测试......
afterMethod......
beforeMethod.......
这是第二个测试......
afterMethod......
afterClass......
afterTest......
PASSED: test1
PASSED: test2