TestNG用法

article/2025/9/21 2:00:03

【bak】https://www.cnblogs.com/uncleyong/p/15855473.html

TestNG简介

单元测试框架,可以用来设计用例的执行流程

创建maven项目,添加依赖

        <dependency><groupId>org.testng</groupId><artifactId>testng</artifactId><version>6.14.3</version><scope>test</scope></dependency>

常用注解

@BeforeSuite / @AfterSuite
@BeforeTest / @AfterTest
@BeforeClass / @AfterClass,在类运行之前/后运行
@BeforeMethod / @AfterMethod,在测试方法之前/后运行
package com.qzcsbj;import org.testng.annotations.*;
import org.testng.annotations.Test;/*** @描述 : <...>* @博客 : www.cnblogs.com/uncleyong* @微信 : ren168632201*/
public class TestAnnotation {@Testpublic  void test(){System.out.println("TestAnnotation.test");System.out.println("线程ID:" + Thread.currentThread().getId());}@Testpublic  void test2(){System.out.println("TestAnnotation.test2");}@BeforeMethodpublic void beforeMethodTest(){System.out.println("TestAnnotation.beforeMethodTest");}@AfterMethodpublic void afterMethodTest(){System.out.println("TestAnnotation.afterMethodTest");}@BeforeClasspublic void beforeClassTest(){System.out.println("TestAnnotation.beforeClassTest");}@AfterClasspublic void afterClassTest(){System.out.println("TestAnnotation.afterClassTest");}@BeforeSuitepublic void beforeSuiteTest(){System.out.println("TestAnnotation.beforeSuiteTest");}@AfterSuitepublic void afterSuiteTest(){System.out.println("TestAnnotation.afterSuiteTest");}
}

输出结果:

TestAnnotation.beforeSuiteTest
TestAnnotation.beforeClassTest
TestAnnotation.beforeMethodTest
TestAnnotation.test
线程ID:1
TestAnnotation.afterMethodTest
TestAnnotation.beforeMethodTest
TestAnnotation.test2
TestAnnotation.afterMethodTest
TestAnnotation.afterClassTest
TestAnnotation.afterSuiteTest

安装插件Create TestNG XML

搜索:Create TestNG XML

 安装

重启后

创建xml文件

在resources下创建suite.xml,文件名随意,只要内容符合要求就可以了
suite:套件,包含一个或多个test
test:测试集,包含一个或多个classes
classes:测试类集合,包含一个或多个class
class:测试类,包含一个或多个方法
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="All Test Suite"><!--<test verbose="2" preserve-order="true" name="test">--><test name="test"><classes><class name="com.qzcsbj.TestAnnotation"/></classes></test><test name="test2"><classes><class name="com.qzcsbj.TestAnnotationB"><methods><include name="testb"/>  <!--指定要运行的方法--></methods></class></classes></test>
</suite>

套件测试

第一个类

package com.qzcsbj;import org.testng.annotations.*;
import org.testng.annotations.Test;/*** @描述 : <...>* @博客 : www.cnblogs.com/uncleyong* @微信 : ren168632201*/
public class TestAnnotation {@Testpublic  void test(){System.out.println("TestAnnotation.test");System.out.println("线程ID:" + Thread.currentThread().getId());}@Testpublic  void test2(){System.out.println("TestAnnotation.test2");}@BeforeMethodpublic void beforeMethodTest(){System.out.println("TestAnnotation.beforeMethodTest");}@AfterMethodpublic void afterMethodTest(){System.out.println("TestAnnotation.afterMethodTest");}@BeforeClasspublic void beforeClassTest(){System.out.println("TestAnnotation.beforeClassTest");}@AfterClasspublic void afterClassTest(){System.out.println("TestAnnotation.afterClassTest");}@BeforeSuitepublic void beforeSuiteTest(){System.out.println("TestAnnotation.beforeSuiteTest");}@AfterSuitepublic void afterSuiteTest(){System.out.println("TestAnnotation.afterSuiteTest");}
}

第二个类

package com.qzcsbj;import org.testng.annotations.*;
import org.testng.annotations.Test;/*** @描述 : <...>* @博客 : www.cnblogs.com/uncleyong* @微信 : ren168632201*/
public class TestAnnotationB {@Testpublic  void testb(){System.out.println("TestAnnotation.testb==");System.out.println("线程ID:" + Thread.currentThread().getId());}@Testpublic  void testb2(){System.out.println("TestAnnotationB.testb2==");}@BeforeMethodpublic void beforeMethodTestb(){System.out.println("TestAnnotationB.beforeMethodTestb==");}@AfterMethodpublic void afterMethodTestb(){System.out.println("TestAnnotationB.afterMethodTestb==");}@BeforeClasspublic void beforeClassTestb(){System.out.println("TestAnnotationB.beforeClassTestb==");}@AfterClasspublic void afterClassTestb(){System.out.println("TestAnnotationB.afterClassTestb==");}@BeforeSuitepublic void beforeSuiteTestb(){System.out.println("TestAnnotationB.beforeSuiteTestb==");}@AfterSuitepublic void afterSuiteTestb(){System.out.println("TestAnnotationB.afterSuiteTestb==");}
}

输出结果:

TestAnnotation.beforeSuiteTest
TestAnnotationB.beforeSuiteTestb==TestAnnotation.beforeClassTest
TestAnnotation.beforeMethodTest
TestAnnotation.test
线程ID:1
TestAnnotation.afterMethodTestTestAnnotation.beforeMethodTest
TestAnnotation.test2
TestAnnotation.afterMethodTest
TestAnnotation.afterClassTestTestAnnotationB.beforeClassTestb==
TestAnnotationB.beforeMethodTestb==
TestAnnotation.testb==
线程ID:1TestAnnotationB.afterMethodTestb==
TestAnnotationB.afterClassTestb==TestAnnotation.afterSuiteTest
TestAnnotationB.afterSuiteTestb==

忽略测试

测试过程中,问题还没解决,可以先忽略,也就是不执行此方法

package com.qzcsbj;import org.testng.annotations.Test;/*** @博客 : www.cnblogs.com/uncleyong* @微信 : ren168632201* @描述 : <...>*/
public class TestIgnore {@Testpublic void testa(){System.out.println("TestIgnore.testa");}@Test(enabled = true)public void testb(){System.out.println("TestIgnore.testb");}@Test(enabled = false)public void testc(){System.out.println("TestIgnore.testc");}
}

运行结果:

TestIgnore.testa
TestIgnore.testb

分组测试

场景︰只想执行个别或者某一部分的测试用例 

package com.qzcsbj;import org.testng.annotations.AfterGroups;
import org.testng.annotations.BeforeGroups;
import org.testng.annotations.Test;/*** @博客 : www.cnblogs.com/uncleyong* @微信 : ren168632201* @描述 : <...>*/
public class TestGroups {@Test(groups = "login")public void testa(){System.out.println("TestIgnore.testa");}@Test(groups = "submitOrder")public void testb(){System.out.println("TestIgnore.testb");}@Test(groups = "submitOrder")public void testc(){System.out.println("TestIgnore.testc");}@BeforeGroups("submitOrder")public void testBeforeGroups(){System.out.println("TestGroups.testBeforeGroups");}@AfterGroups("submitOrder")public void testAfterGroup(){System.out.println("TestGroups.testAfterGroup");}
}

输出结果:

TestIgnore.testa
TestGroups.testBeforeGroups
TestIgnore.testb
TestIgnore.testc
TestGroups.testAfterGroup

xml方式

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="All Test Suite"><!--<test verbose="2" preserve-order="true" name="test">--><test name="test">  <!--test必须有name属性--><groups><run><include name="submitOrder"/></run></groups><classes><class name="com.qzcsbj.TestGroups"/></classes></test>
</suite>

输出结果:

TestGroups.testBeforeGroups
TestIgnore.testb
TestIgnore.testc
TestGroups.testAfterGroup

依赖测试

字符串数组,默认是空

dependsOnMethods和BeforeMethod的区别是: BeforeMethod是每个方法前都要执行,而dependsOnMethods只是依赖的方法前执行

package com.qzcsbj;import org.testng.annotations.Test;/*** @博客 : www.cnblogs.com/uncleyong* @微信 : ren168632201* @描述 : <...>*/
public class TestDepend {@Test(dependsOnMethods = {"test2"})public  void test(){System.out.println("TestAnnotation.test");}@Testpublic  void test2(){System.out.println("TestAnnotation.test2");}
}

运行结果:

TestAnnotation.test2
TestAnnotation.test

如果被依赖方法执行失败,有依赖关系的方法不会被执行;

应用场景,登录失败,就不能进行下单等操作

package com.qzcsbj;import org.testng.annotations.Test;/*** @博客 : www.cnblogs.com/uncleyong* @微信 : ren168632201* @描述 : <...>*/
public class TestDepend {@Test(dependsOnMethods = {"test2"})public  void test(){System.out.println("TestAnnotation.test");}@Testpublic  void test2(){System.out.println("TestAnnotation.test2");throw new RuntimeException();  // 抛出一个异常}
}

运行结果:

TestAnnotation.test2java.lang.RuntimeExceptionat com.qzcsbj.TestDepend.test2(TestDepend.java:19)at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)at java.lang.reflect.Method.invoke(Method.java:498)at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:124)at org.testng.internal.Invoker.invokeMethod(Invoker.java:583)at org.testng.internal.Invoker.invokeTestMethod(Invoker.java:719)at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:989)at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:125)at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:109)at org.testng.TestRunner.privateRun(TestRunner.java:648)at org.testng.TestRunner.run(TestRunner.java:505)at org.testng.SuiteRunner.runTest(SuiteRunner.java:455)at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:450)at org.testng.SuiteRunner.privateRun(SuiteRunner.java:415)at org.testng.SuiteRunner.run(SuiteRunner.java:364)at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52)at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:84)at org.testng.TestNG.runSuitesSequentially(TestNG.java:1208)at org.testng.TestNG.runSuitesLocally(TestNG.java:1137)at org.testng.TestNG.runSuites(TestNG.java:1049)at org.testng.TestNG.run(TestNG.java:1017)at org.testng.IDEARemoteTestNG.run(IDEARemoteTestNG.java:72)at org.testng.RemoteTestNGStarter.main(RemoteTestNGStarter.java:123)

超时

timeout属性的单位为毫秒。

package com.qzcsbj;import org.testng.annotations.Test;
/*** @博客 : www.cnblogs.com/uncleyong* @微信 : ren168632201* @描述 : <...>*/
public class TestTimeOut {@Test(timeOut = 1000)  // 单位为毫秒值,期望在1秒内得到结果public void test() throws InterruptedException {System.out.println("TestTimeOut.test");Thread.sleep(500);}@Test(timeOut = 1000)public void test2() throws InterruptedException {System.out.println("TestTimeOut.test2");for (int i = 10; i > 0; i--) {Thread.sleep(101);System.out.println(i);}System.out.println("执行结束。");}
}

输出结果:

TestTimeOut.test2
10
9
8
7
6
5
4
3
2org.testng.internal.thread.ThreadTimeoutException: Method com.qzcsbj.TestTimeOut.test2() didn't finish within the time-out 1000at java.util.concurrent.locks.AbstractQueuedSynchronizer$ConditionObject.signalAll(AbstractQueuedSynchronizer.java:1953)at java.util.concurrent.ThreadPoolExecutor.tryTerminate(ThreadPoolExecutor.java:716)at java.util.concurrent.ThreadPoolExecutor.processWorkerExit(ThreadPoolExecutor.java:1014)at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1167)at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)at java.lang.Thread.run(Thread.java:748)

断言

package com.qzcsbj;/*** @博客 : www.cnblogs.com/uncleyong* @微信 : ren168632201* @描述 : <...>*/
public class Add {public int sum(int a, int b){return a+b;}
}

package com.qzcsbj;import org.testng.Assert;
import org.testng.annotations.Test;public class MyTest {@Testpublic void test(){Add add = new Add();int actual = add.sum(1, 2);int expect = 2;Assert.assertEquals(actual,expect);}
}

参数化(数据驱动测试)

两种方式向测试方法传递参数:

  利用testng.xml定义parameter

  利用DataProviders

xml文件参数化

package com.qzcsbj;import org.testng.annotations.Parameters;
import org.testng.annotations.Test;/*** @博客 : www.cnblogs.com/uncleyong* @微信 : ren168632201* @描述 : <...>*/
public class TestParameter {@Test@Parameters({"name","id"})public void test(String name, int id){System.out.println("name=" + name + ", id=" + id);}
}

xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="All Test Suite"><!--<test verbose="2" preserve-order="true" name="test">--><test name="test">  <!--test必须有name属性--><classes><class name="com.qzcsbj.TestParameter"><parameter name="name" value="qzcsbj"/><parameter name="id" value="1"/></class></classes></test>
</suite>

运行结果:

name=qzcsbj, id=1

DataProvider参数化

package com.qzcsbj;import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;/*** @博客 : www.cnblogs.com/uncleyong* @微信 : ren168632201* @描述 : <...>*/
public class TestDataProvider {@Test(dataProvider="data")  // 和下面的name对应起来public void testDataProvider(String name, int id){System.out.println("name=" + name + ", id=" + id);}@DataProvider(name = "data")  // 如果没有指定name,上面就写下面的方法名:providerDatapublic Object[][] providerData(){Object[][] datas = new Object[][]{{"zhangsan",1001},{"lisi",1002},{"wangwu",1003}};return datas;}
}

运行结果:

name=zhangsan, id=1001
name=lisi, id=1002
name=wangwu, id=1003

进一步优化,读取excel文件,写一个CaseUtil工具类,根据接口编号来获取这个接口的测试数据,并获取指定列,比如:

Object[][] datas = CaseUtil.getDataByApiId("1",columns);


http://chatgpt.dhexx.cn/article/CjhVBw3h.shtml

相关文章

testNG - 无法访问org.testng.Assert

【异常】无法访问org.testng.Assert 问题表现问题排查问题解决 问题表现 问题排查 报错的是无法访问Assert类&#xff0c;我琢磨着这个类是testNG中很常用的一个类&#xff0c;怎么会找不到&#xff1f; 先从项目的jar包中管理入手&#xff0c;看看有没有其他毛病。 果不其然…

TestNG-学习笔记

https://testng.org/doc/documentation-main.html TestNG概述 TestNG is a testing framework inspired from JUnit and NUnit but introducing some new functionalities that make it more powerful and easier to use, such as: Annotations. Run your tests in arbitrar…

TestNG的使用

testng在maven项目中的使用 pom.xml <dependencies><dependency><groupId>org.testng</groupId><artifactId>testng</artifactId><version>7.4.0</version><scope>test</scope></dependency> </depend…

TestNG

1 TestNG简介 TestNG是Java中的一个测试框架&#xff0c;是一个目前很流行实用的单元测试框架&#xff0c;有完善的用例管理模块&#xff0c;配合Maven能够很方便管理依赖第三方插件。 TestNG消除了大部分的旧框架的限制&#xff0c;使开发人员能够编写更加灵活和强大的测试。…

TestNG自动化测试框架详解

TestNG 文章目录 TestNG一、概述与使用1.1 配置环境1.2 测试方法1.3 使用xml文件 二、测试方法常用注解2.1 配置类注解2.2 非配置类注解2.2.1 Parameters2.2.2 DataProvider 三、依赖测试四、忽略测试五、超时测试六、分组测试七、失败重试机制7.1 IRetryAnalyzer接口7.2 测试方…

TestNG整理

1 基本概念 TestNG:即Testing, Next Generation,下一代测试技术,是根据JUnit和NUnit思想,采用jdk的annotation技术来强化测试功能并借助XML 文件强化测试组织结构而构建的测试框架。最新版本5.12,Eclipse插件最新版本:testng-eclipse-5.12.0.6 TestNG的应用范围: 单…

TestNG使用教程详解

一、TestNG介绍 TestNG是Java中的一个测试框架&#xff0c; 类似于JUnit 和NUnit, 功能都差不多&#xff0c; 只是功能更加强大&#xff0c;使用也更方便。 详细使用说明请参考官方链接&#xff1a;TestNG - Welcome WIKI教程&#xff1a;TestNG - 小组测试( Group Test)_学习…

TestNG单元测试框架详解

目录 前言 ​1. TestNG使用流程 1.1TestNG安装 1.2 创建Maven项目 1.3 Maven配置 1.4 项目TestNG测试类 1.5 运行TestNG 2、TestNG常用注解 3.xml方式运行 3.1 鼠标右击testng.xml运行 3.1 使用maven运行 4. 常用的断言&#xff08;assert&#xff09; 5. TestNG预…

使用ZRender类库画直线、圆弧、曲线以及点在线上的运动

最近在学习Zrender类库&#xff0c;并利用Zrender 让点在直线、圆弧、曲线上运动。大概的写了一些. Zrender是二维绘图引擎&#xff0c;它提供 Canvas、SVG、VML 等多种渲染方式。ZRender 也是 ECharts 的渲染器. 这里我运用的是Canvas画布去画的.想了解Zrender内的属性&…

js画图插件-zrender

zrender&#xff08;Zlevel Render&#xff09; 是一个轻量级的Canvas类库&#xff0c;MVC封装&#xff0c;数据驱动&#xff0c;提供类Dom事件模型&#xff0c;让canvas绘图大不同&#xff01; MVC核心封装实现图形仓库、视图渲染和交互控制&#xff1a; Stroage(M) : shape数…

ZRender文档研读

ZRender文档研读 (基于4.3.2版本) 不使用最新的5.x.x的版本是因为线上文档和最新版本JS文件不匹配-2022年6月13日 1、文档地址 1、官方文档的地址&#xff1a;https://ecomfe.github.io/zrender-doc/public/api.html#zrenderdisplayable 2、Github地址&#xff1a;https://git…

React Developer Tools 下载

React Developer Tools 下载 方法一&#xff1a;网页扩展工具 搜索 React Developer Tools 下载&#xff08;若浏览器不支持搜索React &#xff0c;行不通&#xff09; 打开chrome 浏览器 (只有 chrome 支持 React Developer Tools&#xff09;点击网页工具栏 右上方 确认添…

zrender TypeError: “x“ is not a constructor

如果有兴趣了解更多相关内容&#xff0c;欢迎来我的个人网站看看&#xff1a;耶瞳空间 我是调用zrender的init方法报错&#xff0c;如下图&#xff1a; 然后经过大佬指点&#xff0c;这种开发环境没问题但生产环境报错的东西&#xff0c;一般是因为打包的时候被tree-shaking…

vue+zrender实现医院体温单

项目背景 医院医护项目需求&#xff0c;需要用H5做一个通用的体温单 项目演示 版本一 版本二 项目代码简介 由vue-cli4脚手架快速搭建生成&#xff0c;主要代码都在thermometer.vue文件里面&#xff0c;后续修改也主要是在这个文件修改。 项目需求难点在中间的网格部分…

ZRender开发

ZRender开发 开发文档&#xff1a;https://ecomfe.github.io/zrender-doc/public/ <!-- eslint-disable no-undef --> <template><div class"config-page"><div class"header"><el-button click"handleAdd">撒…

Echarts 源码解读 一:zrender源码分析1var zr = zrender.init(document.getElementById(‘main‘));

2021SCSDUSC 因为Echarts是基于zrender进行实现的&#xff0c;所以解读echarts源码前&#xff0c;首先要对zrender有基本的了解。 zrender是canvas的一个类库&#xff0c;zrender是基于canvas实现的。 目录 zrender的src文件夹 文件夹&#xff1a; animation动画相关 cont…

高效canvas绘图框架——zrender

一个轻量级的Canvas类库&#xff0c;MVC封装&#xff0c;数据驱动&#xff0c;提供类Dom事件模型&#xff0c;让canvas绘图大不同 Architecture MVC核心封装实现图形仓库、视图渲染和交互控制&#xff1a; Stroage(M) : shape数据CURD管理Painter(V) : canvase元素生命周期管理…

轻量级的Canvas类库zrender使用笔记 :简单自定义图件开发

ECharts&#xff0c;一个纯 Javascript 的图表库&#xff0c;底层依赖轻量级的 Canvas 类库 ZRender&#xff0c;提供直观&#xff0c;生动&#xff0c;可交互&#xff0c;可高度个性化定制的数据可视化图表。当然我们自己可能有些需求&#xff0c;通过修改ECharts或者highchar…

vue-echarts的ZRender事件

首先贴出 vue-echarts 的官网连接&#xff1a;https://www.npmjs.com/package/vue-echarts 1、vue-echarts 具体在vue中的使用方法&#xff0c;本文中就暂时不多介绍了&#xff1b; 2、如何在vue中使用vue-echarts的鼠标事件&#xff1a; 具体使用方式跟vue中使用echarts一样&a…

ZRender文档研读 (基于4.3.2版本)

ZRender文档研读 &#xff08;基于4.3.2版本&#xff09; 不使用最新的5.x.x的版本是因为线上文档和最新版本JS文件不匹配-2022年6月13日 1、文档地址 1、官方文档的地址&#xff1a;https://ecomfe.github.io/zrender-doc/public/api.html#zrenderdisplayable 2、Github地址…