TestNg测试框架使用
doc :http://testng.org/doc/documentation-main.html
github: https://github.com/cbeust/testng
IDEA : http://testng.org/doc/idea.html
eclipse : http://testng.org/doc/eclipse.html
安装Eclipse插件
For Eclipse 3.4 and above, enter http://beust.com/eclipse.
For Eclipse 3.3 and below, enter http://beust.com/eclipse1.
Maven支持TestNG
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.homeinns.web</groupId> <artifactId>homeinns-testng</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <name>homeinns-testng</name> <url>http://maven.apache.org</url> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> </properties> <dependencies> <dependency> <groupId>org.testng</groupId> <artifactId>testng</artifactId> <version>6.1.1</version> <scope>test</scope> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-surefire-plugin</artifactId> <version>2.16</version> <configuration> <suiteXmlFiles> <suiteXmlFile>testng.xml</suiteXmlFile> <!-- <suiteXmlFile>src/test/resources/testng.xml</suiteXmlFile> --> </suiteXmlFiles> </configuration> </plugin> </plugins> </build> </project>
配置TestNg suite
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd"> <suite name="Suite" parallel="none"> <!--enabled="true"让测试生效,也可根据情况关闭某些测试 --> <test name="Test" enabled="true"> <!--指定参数 --> <parameter name="Name" value="Irving" /> <parameter name="Sex" value="Man" /> <!--指定测试包 --> <packages> <package name="com.homeinns.web.testng.*" /> </packages> <!--指定测试类 --> <classes> <class name="com.homeinns.web.testng.AppTest" /> </classes> </test> <!-- Test --> </suite> <!-- Suite -->
TestNg注解配置
public class NgTest { @Test public void f() { } @Test( // 在指定的时间内启用3个线程并发测试本方法10次 threadPoolSize = 3, invocationCount = 10, timeOut = 10000, // 等待测试方法t0测试结束后开始本测试 dependsOnMethods = { "f" }, // 指定测试数据源CLASS和数据源名称(参考注解@DataProvider),返回几条数据会跑测试方法几次 dataProvider = "generate", dataProviderClass = GeneratorRandomNum.class, // 分组名称 groups = { "checkin-test" }) // 读取配置文件中的参数,配置如上,用@Optional设置默认值 @Parameters({ "Name" }) public void f1(@Optional("name") String name) { } }
测试报告
运行测试后 在my-testng/test-output/ 目录下(maven \target\surefire-reports)
gradle配置
subprojects {
apply plugin: 'java'
// Disable the test report for the individual test task
test {
reports.html.enabled = false
}
}
task testReport(type: TestReport) {
destinationDir = file("$buildDir/reports/allTests")
//Include the results from the `test` task in all subprojects
reportOn subprojects*.test
}Grouping TestNG tests
test {
useTestNG {
excludeGroups 'integrationTests'
includeGroups 'unitTests'
}
}
Refer:
http://maven.apache.org/surefire/maven-surefire-plugin/examples/testng.html#
JUnit 4 Vs TestNG – Comparison
http://www.mkyong.com/unittest/junit-4-vs-testng-comparison/
TestNG Tutorial
http://www.mkyong.com/tutorials/testng-tutorials/
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· .NET周刊【3月第1期 2025-03-02】
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· [AI/GPT/综述] AI Agent的设计模式综述