一、testng 介绍
TestNG 是一个测试框架,其灵感来自 JUnit 和 NUnit,但同时引入了一些新的功能,使其功能更强大,使用更方便。
TestNG 设计涵盖所有类型的测试:单元,功能,端到端,集成等,它需要 JDK5 或更高的 JDK 版本。
详细使用说明请参考官方链接:https://testng.org/doc/index.html
在 maven 中引入依赖:
org.testng
testng
6.10
简单示例:
(1)被测试 HelloWorld 类
package study.testng;
public class HelloWorld {
public String hello(){
return "hello world !";
}
}
(2)测试类 TestHelloWorld 类
package study.testng;
import org.testng.Assert;
import org.testng.annotations.Test;
public class TestHelloWorld {
//测试返回结果不为空
@Test
public void tester1(){
HelloWorld hello = new HelloWorld();
String helloworld = hello.hello();
Assert.assertNotNull(helloworld);
}
//测试返回结果为”hello world !“字符串
@Test
public void tester2(){
HelloWorld hello = new HelloWorld();
String helloworld = hello.hello();
System.out.println(helloworld);
Assert.assertEquals(helloworld, "hello world !");
}
}
(3)测试结果
[TestNG] Running:
C:UsersAdministrator.IntelliJIdea2019.2systememp-testng-customsuite.xml
hello world !
===============================================
Default Suite
Total tests run: 2, Failures: 0, Skips: 0
===============================================
二、@Test注解及常用属性
凡是在类方法中添加了 @Test 注解的就是我们需要测试的方法
1、enable 测试方法是否执行
默认是 true , 如果设置为 false ,则在运行时不会执行这个测试方法;
示例:
package com.ggf.testng.annotation;
import org.testng.annotations.Test;
/**
* @Description: 忽略测试,可以通过@Test的注解的enable属性来配置是否执行用例方法
* enable默认值为 true,需要设置为false才会跳过这个测试用例
* @Author: ggf
* @Date: 2019/12/29
*/
public class IgnoreTest {
@Test
public void ignore1() {
System.out.println("ignore1 run...");
}
@Test(enabled = false)
public void ignore2() {
System.out.println("ignore2 run ...");
}
@Test
public void ignore3() {
System.out.println("ignore3 run ...");
}
}
运行结果:
[TestNG] Running:
C:UsersAdministrator.IntelliJIdea2019.2systememp-testng-customsuite.xml
ignore1 run...
ignore3 run ...
===============================================
Default Suite
Total tests run: 2, Failures: 0, Skips: 0
===============================================
2、预期异常expectedExeption
@Test(expectedExceptions = ClassName.class)
如果 ClassName 类抛出了异常,测算测试通过,没有异常算测试不通过;
expectedExceptions的值也可以是一个数组:
@Test(expectedExceptions = {ClassName.class, ClassName2.class,... })
示例:
package com.ggf.testng.annotation;
import org.testng.annotations.Test;
/**
* @Description: 异常测试
* @Author: ggf
* @Date: 2019/12/29
*
* 什么时候会用到异常测试??
* 在我们期望结果为某一个异常的时候
* 比如:我们传入了某些不合法的参数,程序抛出了异常
* 也就是说我的期望结果就是这个异常。
*/
public class ExpectedException {
/**
* 运行时异常,我们期望返回一个运行时异常,这条用例才是正确的。
*/
@Test(expectedExceptions = RuntimeException.class, enabled = false)
public void runTimeExceptionFailed() {
System.out.println("没有抛出异常,这条用例不通过!");
}
/**
* 结果抛出了一个运行时异常,和我们的期望一致,测试通过。
*/
@Test(expectedExceptions = RuntimeException.class)
public void runTimeExceptionSuccess() {
System.out.println("程序抛出了运行时异常,测试用例通过!");
throw new RuntimeException();
}
}
运行结果:
[TestNG] Running:
C:UsersAdministrator.IntelliJIdea2019.2systememp-testng-customsuite.xml
程序抛出了运行时异常,测试用例通过!
===============================================
Default Suite
Total tests run: 1, Failures: 0, Skips: 0
===============================================
3、依赖方法dependsOnMethods
在被依赖的方法运行完成之后运行当前方法,如果依赖方法测试不通过,那么当前方法也不会继续运行了;依赖的方法可以