TestNG学习
什么是TestNG
testNG是Java中的一个测试框架,类似于JUnit 和NUnit, 功能都差不多, 只是功能更加强大,使用也更方便。可进行单元测试,功能测试,端到端测试,集成测试等相关测试。
-注解。
-在具有各种可用策略的任意大线程池中运行测试(所有方法都在各自的线程中,每个测试类一个线程,等等)。
-测试您的代码是多线程安全的。
-灵活的测试配置。
-支持数据驱动的测试(使用@DataProvider)。
-支持参数。
-强大的执行模型(不再需要TestSuite)。
-由各种工具和插件(Eclipse,IDEA,Maven等)支持。
-嵌入BeanShell以获得更高的灵活性。
-用于运行时和日志记录的默认JDK函数(无依赖项)。
-应用程序服务器测试的相关方法。
添加pom maven依赖
创建一个maven工程 在pom.xml中添加下面的依赖
<dependencies>
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<version>6.14.3</version>
</dependency>
</dependencies>
Idea创建module
在java package下创建以公司域名为包名的package 并创建java class(命名testCase)
创建方法。。。
在方法上添加@Test注解,将方法标记为测试的一部分
import org.testng.annotations.*;
public class BasicAnnotation {
@Test
public void testCase(){
System.out.println("测试testng");
}
@BeforeMethod
public void testBefore(){
System.out.println("测试testBefore");
}
@AfterMethod
public void testAfter(){
System.out.println("测试testAfter");
}
@BeforeSuite
public void testSuiteBefore(){
System.out.println("测试SuiteBefore");
}
@AfterSuite
public void testSuiteAfter(){
System.out.println("测试SuiteAfter");
}
}
如果@Test注解为红色时,说明没有import对应的包。
我们可以这样操作:
File—>settings—>Genseral---->Auto Import 将一下勾选框勾选

注解之@BeforeMethod和@AfterMethod
在类中可以影响方法的调用顺序
@Test
public void testCase(){
System.out.println("测试testng");
}
@BeforeMethod
public void testBefore(){
System.out.println("测试testBefore");
}
@AfterMethod
public void testAfter(){
System.out.println("测试testAfter");
}
执行结果

注解之@BeforeClass和@AfterClass
在类运行之前和之后执行的注解
@Test
public void testCase(){
System.out.println("测试testng");
}
@BeforeMethod
public void testBefore(){
System.out.println("测试testBefore");
}
@AfterMethod
public void testAfter(){
System.out.println("测试testAfter");
}
@BeforeClass
public void testClassBefore(){
System.out.println("测试testClassBefore");
}
@AfterClass
public void testClassAfter(){
System.out.println("测试testClassAfter");
}
执行结果

注解之@BeforeSuite和@AfterSuite
执行顺序中一个重要的注解(测试套件注解):suite把class包裹起来,suite可以包含多个class,类是属于某个套件之下的。
@Test
public void testCase(){
System.out.println("测试testng");
}
@BeforeMethod
public void testBefore(){
System.out.println("测试testBefore");
}
@AfterMethod
public void testAfter(){
System.out.println("测试testAfter");
}
@BeforeClass
public void testClassBefore(){
System.out.println("测试testClassBefore");
}
@AfterClass
public void testClassAfter(){
System.out.println("测试testClassAfter");
}
@BeforeSuite
public void testSuiteBefore(){
System.out.println("测试SuiteBefore");
}
@AfterSuite
public void testSuiteAfter(){
System.out.println("测试SuiteAfter");
}
执行结果

忽略测试(enabled = false)
在@Test注解标签后添加属性(enabled = false)
@Test
public void testCase(){
System.out.println("测试testng");
}
@BeforeMethod(enabled = false)
public void testBefore(){
System.out.println("测试testBefore");
}
@AfterMethod(enabled = false)
public void testAfter(){
System.out.println("测试testAfter");
}
@BeforeClass
public void testClassBefore(){
System.out.println("测试testClassBefore");
}
@AfterClass
public void testClassAfter(){
System.out.println("测试testClassAfter");
}
执行结果

分组测试
方法上分组,是将方法进行分组
@Test
public void testCase(){
System.out.println("测试testng");
}
@Test(groups = "one")
public void testGroupOne(){
System.out.println("测试testGroupOne");
}
@Test(groups = "one")
public void testGroupTwo(){
System.out.println("测试testGroupTwo");
}
@Test(groups = "two")
public void testGroupThree(){
System.out.println("测试testGroupThree");
}
@BeforeMethod
public void testBefore(){
System.out.println("测试testBefore");
}
@AfterMethod
public void testAfter(){
System.out.println("测试testAfter");
}
@BeforeClass
public void testClassBefore(){
System.out.println("测试testClassBefore");
}
@AfterClass
public void testClassAfter(){
System.out.println("测试testClassAfter");
}
执行结果

参数化测试 -xml文件参数化
在resource目录下,新建一个xml文件。邮件xml文件点击run即可运行。




执行结果:


本文详细介绍了TestNG测试框架,包括它的功能特性,如注解使用,如@BeforeMethod、@AfterMethod、@BeforeClass、@AfterClass、@BeforeSuite和@AfterSuite,以及如何进行分组测试和参数化测试。通过添加maven依赖和在IDEA中创建module,演示了如何在Java项目中集成和使用TestNG进行单元测试。
3565

被折叠的 条评论
为什么被折叠?



