TestNG-Java自动化测试快速入门
基本了解
TestNG依赖
<!-- https://mvnrepository.com/artifact/org.testng/testng -->
<dependency><groupId>org.testng</groupId><artifactId>testng</artifactId><version>7.11.0</version><scope>test</scope>
</dependency>
TestNG的管理等级
Suite->test->class->method
建议的管理层级
class对应一个测试用例,suite作为测试集
管理执行层级逻辑顺序
TestNG常用注解
@BeforeSuite
在此套件中的所有测试运行之前,带注解的方法将只运行一次
@AfterSuite
在此套件中的所有测试运行之后,带注解的方法将只运行一次
@BeforeClass
在调用当前类的第一个测试方法之前,带注解的方法将只运行一次
@AfterClass
在当前类的所有测试方法都运行后,带注解的方法将只运行一次
@BeforeTest
带注解的方法将在任何属于<test>标签内的类的测试方法之前执行
@AfterTest
带注解的方法将在任何属于<test>标签内的类的所有测试方法运行后运行
@BeforeGroups
此配置方法之前将运行的组列表。此方法保证在调用属于任何这些组的第一个测试方法之前不久运行
@AfterGroups
此配置方法将在其后运行的组列表。此方法保证在调用属于任何这些组的最后一个测试方法后不久运行
@BeforeMethod
注解方法将在每个测试方法之前运行
@AfterMethod
注解方法将在每个测试方法之后运行
@DataProvider
将方法标记为测试方法提供数据。带注解的方法必须返回一个Object【】【】,其中每个Object【】【】都可以分配测试方法的参数列表。想要从这个DataProvider接收数据的@Test方法需要使用一个与这个注解的名称相等的dataProvider名称
@Factory
将方法标记为工厂,该工厂返回将由TestNG用作测试类的对象。该方法必须返回Object【】【】
@Listeners
在测试类上定义监听器
@Parameters
描述如何将参数传给@Test方法
@Test
将类或方法标记为测试的一部分
注解的执行流程
@BeforeSuite -> @BeforeTest -> @BeforeGroups -> @BeforeClass -> @BeforeMethod
-> 测试方法 ->
@AfterMethod -> @AfterClass -> @AfterGroups -> @AfterTest -> @AfterSuite
Before-After注解中的参数
注解介绍
@BeforeSuite @AfterSuite
@BeforeClass @AfterClass
@BeforeTest @AfterTest
@BeforeGroups @AfterGroups
@BeforeMethod @AfterMethod
参数介绍
alwaysRun
对于before(@BeforeSuite @BeforeClass @BeforeTest @BeforeMethod但不是 @BeforeGroups)方法:
如果设置为true,那么无论它属于哪个组,都会运行此配置方法
对于after方法:
如果设置为true,则即使之前被调用的一个或多个方法失败或被跳过,此配置方法也将执行
dependsOnGroups
此方法所依赖的组列表
dependsOnMethods
此方法所依赖的方法列表,也就是我们这个方法执行之前我们要先执行xxx方法
enabled
是否启动此类/方法上的方法
groups
这个类/方法所属的组列表
inheriteGroups
如果为true,则此方法将属于在类级别的@Test批注中指定的组
onlyForGroups
仅适用于@BeforeMethod和@AfterMethod。
如果指定,则仅当相应的测试方法属于列出的组之一时才会调用此设置/拆卸方法
@Factory注解
注解介绍
@Factory 注解方法允许在运行时根据某些数据集或条件创建测试
该方法必须返回 Object []
使用例子
本来我们的Test只执行一次,但是我们用了我们的Factory注解进行配置
这样子我们执行我们的Test的时候就会根据我们的Factory配置中,配置的执行两次,让我们的Test执行两次
package com.example.testng;import org.testng.annotations.Factory;
import org.testng.annotations.Test;public class SimpleTestFactory {@Testpublic void testMethod(){System.out.println("Simple Test Method.");}@Factorypublic Object[] factoryMethod() {return new Object[]{new SimpleTestFactory(),new SimpleTestFactory()};}}
输出结果
@Listener注解
注解介绍
@Listeners 注解在测试类上定义侦听器
@Listeners 注释方法侦听某些事件并跟踪测试执行,同时在测试执行的每个阶段执行某些操作
事件可以是任何事情,例如测试方法成功、测试方法失败、测试方法开始等
监听器or接口
以下是一些允许您修改 TestNG 行为的侦听器或接口:
- IAnnotationTransformer
- IAnnotationTransformer2
- IHookable
- IInvokedMethodListener
- IMethodInterceptor
- IReporter
- ISuiteListener
- ITestListener
参数
value
扩展 org.testng.ITestNGListener 的类数组
使用例子
监听器类
package com.example.testng;import org.testng.ITestContext;
import org.testng.ITestListener;
import org.testng.ITestResult;public class TestListener implements ITestListener {@Overridepublic void onTestStart(ITestResult result) {System.out.println("测试开始了");}@Overridepublic void onTestSuccess(ITestResult result) {System.out.println("测试用例成功,其详情为:" + result.getName());}@Overridepublic void onTestFailure(ITestResult result) {System.out.println("测试用例失败,其详情为:" + result.getName());}@Overridepublic void onTestSkipped(ITestResult result) {System.out.println("测试用例被跳过,其详情为:" + result.getName());}@Overridepublic void onTestFailedButWithinSuccessPercentage(ITestResult result) {System.out.println("测试用例失败,但在成功百分比范围内,其详情为:" + result.getName());}@Overridepublic void onStart(ITestContext context) {System.out.println("整个测试开始前被调用");}@Overridepublic void onFinish(ITestContext context) {System.out.println("整个测试结束后被调用");}
}
使用Listener注解标记使用的监听器
package com.example.testng;import org.testng.Assert;
import org.testng.annotations.Listeners;
import org.testng.annotations.Test;
@Listeners({TestListener.class})public class SimpleTestListener {@Testpublic void sum(){int sum=0;int a=15;int b=27;sum=a+b;System.out.println("sum="+sum);}@Testpublic void testtofail(){System.out.println("Test case has failed");Assert.assertTrue(false);}}
@Parameters注解
注解介绍
@Parameters 描述了如何将参数传递给 @Test 方法
参数
value
用于填充此方法参数的变量列表
@Parameters 注释可以放置在以下位置:
在任何已经有 @Test、@Before/After 或 @Factory 注释的方法上
在测试类的最多一个构造函数上。每当需要实例化您的测试类时,TestNG 将使用初始化为 testng.xml 中指定值的参数调用此特定构造函数。此功能可用于将类中的字段初始化为随后将被测试方法使用的
@Test注解中的参数介绍
alwaysRun
如果设置为 true,则此测试方法将始终运行,即使它依赖于失败的方法
dataProvider
此测试方法的数据提供者的名称。
dataProviderClass
在哪里寻找数据提供者的类。如果未指定,数据提供者将在当前测试方法的类或其基类之一上查找。如果指定了此属性,则数据提供程序方法需要在指定的类上是静态的。
dependsOnGroups
此方法所依赖的组列表
dependsOnMethods
此方法所依赖的方法列表
description
此方法的说明
enabled
是否启用此类 / 方法上的方法
expectedExceptions
测试方法预期抛出的异常列表。如果没有抛出异常或与此列表中的异常不同,则此测试将被标记为失败
groups
这个类 / 方法所属的组列表
invocationCount
应调用此方法的次数,控制执行测试用例的次数
invocationTimeOut
对于所有调用计数的累计时间,此测试应花费的最大毫秒数。如果未指定 invocationCount,则该属性将被忽略
priority
此测试方法的优先级。将首先安排较低的优先级
successPercentage
此方法预期的成功百分比
singleThreaded
如果设置为 true,则此测试类上的所有方法都保证在同一线程中运行。此属性只能在类级别使用,如果在方法级别使用,它将被忽略
timeOut
此测试应花费的最大毫秒数
threadPoolSize
此方法的线程池大小。该方法将从 invocationCount 指定的多个线程中调用
断言
其实相当于一个判断类,里面提供了很多方法
Assert.assertEquals();
Assert.assertTrue();
批量执行测试用例-使用testng.xml文件
测试案例一
package com.example.testng;import org.testng.annotations.Test;public class BatchTest1 {@Testpublic void batchTest(){}
}
测试案例二
package com.example.testng;import org.testng.annotations.Test;public class BatchTest2 {@Testpublic void batchTest(){}
}
testng.xml文件
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "https://testng.org/testng-1.0.dtd">
<suite name="KIRA"><test name="批量测试1"><classes><class name="com.example.testng.BatchTest1"/></classes></test><test name="批量测试2"><classes><class name="com.example.testng.BatchTest2"/></classes></test>
</suite>
ps:testng.xml文件是和pom文件是一个层级的,而不是和resource是一个层级的
右键testng.xml文件,然后运行
运行结果
testng.xml结合@Parameters注解实现数据驱动
简单传值(不常用)
在testng.xml中定义参数的值
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "https://testng.org/testng-1.0.dtd"><suite name="KIRA">
<test name="数据驱动"><classes><class name="com.example.testng.ParamsTest"/><parameter name="username" value="Kira"></parameter><parameter name="password" value="123456"></parameter></classes>
</test></suite>
通过@Parameters注解来引入参数
package com.example.testng;import org.testng.annotations.BeforeClass;
import org.testng.annotations.Parameters;
import org.testng.annotations.Test;public class ParamsTest {@BeforeClasspublic void start(){System.out.println("我们开始测试我们的登录");}@Test@Parameters(value = {"username","password"})public void Test(String username,String password){System.out.println("用户名:"+username);System.out.println("密码:"+password);}
}
我们右键testng.xml文件来执行
执行结果
@DataProvider注解提供测试用例
DataProvider注解构建测试用例
package com.example.testng;import org.testng.annotations.BeforeClass;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Parameters;
import org.testng.annotations.Test;public class DataProviderTest {@DataProvider(name = "paramData")public Object[][] provideData() {return new Object[][] {{"Kira", "123456"},{"Mikasa","654321"}};}@BeforeClasspublic void start(){System.out.println("我们开始测试我们的登录");}@Test(dataProvider = "paramData")public void Test(String username,String password){System.out.println("用户名:"+username);System.out.println("密码:"+password);}
}
执行结果