maven使用TestNG
maven配置TestNG
如需要使用TestNG,需要在工程里添加依赖(可以替换你想使用的版本)
<dependencies>[...]<dependency><groupId>org.testng</groupId><artifactId>testng</artifactId><version>6.9.8</version><scope>test</scope></dependency>[...]
</dependencies>
如果使用的TestNG的版本过低(<=5.11),依赖的书写形式如下:
<dependencies>[...]<dependency><groupId>org.testng</groupId><artifactId>testng</artifactId><version>5.11</version><scope>test</scope><classifier>jdk15</classifier></dependency>[...]
</dependencies>
注意:如果使用JDK1.4 JavaDoc注释进行TestNG测试,需要用jdk14替换上面的classifier属性中的jdk15
在pom中添加依赖是必须的步骤
XML文件使用套件
另一种替代方法是使用TestNG套件XML文件。这允许灵活配置的测试运行。以正常的方式创建这些文件,然后添加到成功的插件配置:
<plugins>[...]<plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-surefire-plugin</artifactId><version>2.19</version><configuration><suiteXmlFiles><suiteXmlFile>testng.xml</suiteXmlFile></suiteXmlFiles></configuration></plugin>[...]
</plugins>
配置将覆盖包括和不包括模式和运行所有测试套件的文件。
指定测试参数
TestNG测试可以接受参数@Parameters 注释。你也可以从Maven将参数传递到你TestNG测试,通过指定系统属性,eg:
<plugins>[...]<plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-surefire-plugin</artifactId><version>2.19</version><configuration><systemPropertyVariables><propertyName>firefox</propertyName></systemPropertyVariables></configuration></plugin>[...]
</plugins>
使用分组
TestNG允许将测试分组,分组后你可以选择执行一个多个组,用surefire带分组参数eg
<plugins>[...]<plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-surefire-plugin</artifactId><version>2.19</version><configuration><groups>functest,perftest</groups></configuration></plugin>[...]
</plugins>
同样,excludedGroups参数可以在执行的时候排除一些组,使之不执行
并行执行测试
TestNG允许并行执行测试用例,并行执行必须配合parallel参数,默认是5个线程,可以通过配置threadCount属性修改默认值。
eg:
</plugins>[...]<plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-surefire-plugin</artifactId><version>2.19</version><configuration><parallel>methods</parallel><threadCount>10</threadCount></configuration></plugin>[...]
</plugins>
对于高并发,或需要快速评估线程和代码的测试是十分有用的。
TestNG 版本5.10+、插件版本2.19+并行测试时允许使用data provider
</plugins>[...]<plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-surefire-plugin</artifactId><version>2.19</version><configuration><properties><property><name>parallel</name><value>methods</value></property><property><name>dataproviderthreadcount</name><value>30</value></property></properties></configuration></plugin>[...]
</plugins>
eg:
</plugins>[...]<plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-surefire-plugin</artifactId><version>2.19</version><configuration><suiteXmlFiles><file>src/test/resources/testng1.xml</file><file>src/test/resources/testng2.xml</file></suiteXmlFiles><properties><property><name>suitethreadpoolsize</name><value>2</value></property></properties></configuration></plugin>[...]
</plugins>
使用自定义侦听器和报告
TestNG支持附加自定义侦听器,报告,注解和方法拦截器。默认情况下,TestNG附带一些基本的监听生成HTML和XML报告。
你可以配置多个自定义侦听器是这样的:
<dependencies>
[...]<dependency><groupId>your-testng-listener-artifact-groupid</groupId><artifactId>your-testng-listener-artifact-artifactid</artifactId><version>your-testng-listener-artifact-version</version><scope>test</scope></dependency>
[...]
</dependencies>
[...]
</plugins>[...]<plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-surefire-plugin</artifactId><version>2.19</version><configuration><properties><property><name>usedefaultlisteners</name><value>false</value></property><property><name>listener</name>
<value>com.mycompany.MyResultListener,com.mycompany.MyAnnotationTransformer,com.mycompany.MyMethodInterceptor</value></property><property><name>reporter</name><value>listenReport.Reporter</value></property></properties></configuration></plugin>[...]
</plugins>
更多信息请参考testng官网
定制TestNG对象工厂
自从插件版本2.19和TestNG 5.7或更高版本你可以定制通过实现org.testng.IObjectFactory TestNG的对象工厂。并绑定类名。
</plugins>[...]<plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-surefire-plugin</artifactId><version>2.19</version><configuration>[...]<properties><property><name>objectfactory</name>
<value>testng.objectfactory.TestNGCustomObjectFactory</value></property></properties>[...]</configuration></plugin>[...]
</plugins>
定制TestNG 运行工厂
从插件v2.19+和TestNG v5.9+,可以通过实现org.testng.ITestRunnerFactory实现定制TestNG runner factory,并绑定类名
</plugins>[...]<plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-surefire-plugin</artifactId><version>2.19</version><configuration>[...]<properties><property><name>testrunfactory</name>
<value>testng.testrunnerfactory.TestNGCustomTestRunnerFactory</value></property></properties>[...]</configuration></plugin>[...]
</plugins>
根据”标签名称”运行测
测试定义的标签匹配配置中的标签将会被执行,否则不会被执行。在以下的7个用例中只会被执行两个:即InstallTest和ATest。测试标签a-t3在suite.xml不能够匹配,不执行任何测试。
</plugins>[...]<plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-surefire-plugin</artifactId><version>2.19</version><configuration>[...]<suiteXmlFiles><file>src/test/resources/suite.xml</file></suiteXmlFiles><properties><property><name>testnames</name><value>a-t1,a-t3</value></property></properties>[...]</configuration></plugin>[...]
</plugins>
The test suite ‘suite.xml’ :
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >
<suite name="Component Tests" verbose="2" annotations="JDK"><test name="a-t1" preserve-order="true" ><classes><class name="server.InstallTest" /><class name="server.ATest" /></classes></test><test name="a-t2" preserve-order="true" ><classes><class name="server.SCHTest" /><class name="server.PRGTest" /><class name="server.SIBBTest" /><class name="server.EDNTest" /><class name="server.PPVTest" /></classes></test>
</suite>
同时运行TestNG和JUnit
TestNG 6.5.1 以上的版本支持同时运行TestNG 和 JUnit 4.x在同一个maven项目中 .
<dependencies>[...]<dependency><groupId>org.testng</groupId><artifactId>testng</artifactId><version>6.9.4</version><scope>test</scope></dependency><dependency><groupId>junit</groupId><artifactId>junit</artifactId><version>4.10</version><scope>test</scope></dependency>[...]
</dependencies>
Testing和junit同时运行需要注意版本
备忘(还未完全整理,可能会有不恰当之处)
maven使用TestNG相关推荐
- java接口自动化Excel占位符_基于maven+java+TestNG+httpclient+poi+jsonpath+ExtentReport的接口自动化测试框架...
接口自动化框架 项目说明 本框架是一套基于maven+java+TestNG+httpclient+poi+jsonpath+ExtentReport而设计的数据驱动接口自动化测试框架,TestNG ...
- maven 执行testng.xml文件失败解决问题
maven 执行testng.xml文件失败解决问题 参考文章: (1)maven 执行testng.xml文件失败解决问题 (2)https://www.cnblogs.com/woniu123/p ...
- 用maven搭建 testNG+PowerMock+Mockito测试框架
转载:http://www.cnblogs.com/changzhz/p/5158068.html 单元测试是开发中必不可少的一部分,是产品代码的重要保证. Junit和testNG是当前最流行的测试 ...
- Jenkins+maven(testng)项目(本地项目配置)
一.前提: 1. Jenkins下载安装. 2. testng的Maven项目. 二.检测: maven项目自身配置及检测. 1. POM.XML配置文件增加: <build> <p ...
- 【jenkins】jenkins+maven+gitlab+testng,jenkins配置
电脑版本:windows10企业版 jenkins配置: 1.general配置,这里的配置比较简单,基本默认就可以了 2.源码管理 2.1填写git地址,从你的gitlib项目里去找.不会的自行百度 ...
- Eclipse+Maven配置TestNG框架
目录 前言 准备工作 一.配置Maven 1.1 settings.xml文件 1.2 在Eclipse中单击"Window>Proferences>Maven>User ...
- maven使用testng_使用Maven Failsafe和TestNG分别运行单元测试和集成测试
maven使用testng 最近,对于我的新宠物项目,我决定我希望在标准mvn测试期间执行一些测试,而仅在不同阶段执行一些其他测试 ,我们称其为集成阶段. 我在谷歌上搜索,似乎没有任何工作,因此在努力 ...
- maven使用testng_使用ReportNG更好看的TestNG HTML测试报告– Maven指南
maven使用testng 当"扩展TestCase"是编写测试中必不可少的部分时, TestNG是作为JUnit 3的注释驱动替代创建的测试框架. 即使到现在,它也提供了一些有趣 ...
- java testng 项目_java – Junit4和TestNG在Maven的一个项目中
要将它们一起运行,可用的选项很少,但我选择了为Junit和TestNG使用不同的配置文件.但现在的问题是排除和包括测试用例. 因为如果我们在maven中将testNG依赖项添加到主项目,它将跳过所有J ...
最新文章
- The Six Best Practices(1~3)
- UIWebView滚动监听
- clodeblocks debug断点调试_Go 的 Debug 工具 delve 介绍
- 【职场】清华同学在鹅厂五星绩效,还是失业了!
- 【Geek软技能】程序员,为什么写不好一份简历?
- 【Spring学习】Spring JdbcTemplate之五类方法总结
- python param_Python基于paramunittest模块实现excl参数化
- NOIP模拟测试5「星际旅行·砍树·超级树」
- HTTP和HTTPS的请求和响应
- iPhone 之后,苹果还会带来什么?
- 生成XML文件的步骤 解析XML文件
- mysql 执行顺序_MySQL 基础知识掌握
- h5 游戏 游戏框架 Phaser
- Linux搭建Weblogic集群
- docx行间距怎么设置_word2017如何设置行间距.docx
- matlab编写的程序输入参数怎么写,MATLAB|标准参数输入对话框创建
- 网页自动加拼音html,javascript实现输入中文自动生成拼音
- 实战三:手把手教你实现物体识别
- 包裹侠快递查询_全球顶尖技术精英汇聚菜鸟 准备帮助快递攻破体积测量难题...
- Nginx配置虚拟主机(基于域名、端口及IP)