测试开发 | 使用Extent report美化单元测试框架TestNG的报告

jUnit是Java领域事实上的单元测试框架标准,但是TestNG也有其特点,所以在单元测试或者自动化测试里面也占有一席之地,从google趋势上看,大体是3:1:

测试开发 | 使用Extent report美化单元测试框架TestNG的报告

 

前面的文章我们提到,Allure作为开源的测试报告框架,原生提供了对TestNG的支持;Extent report作为另外一款常用报告框架,也通过适配器(extentreports-testng-adapter)的形式提供了TestNG的支持,注意,没有jUnit哦:)下面的文章会覆盖如下几个点:

  • 从github上fork源码并修改、打包、部署到nexus私服
  • extentreports-testng-adapter在TestNG的使用

源码修改

前面的文章开源报告框架Extent report的使用 我们提到,默认的Extent report生成之后使用的css链接地址在国内无法访问,我们需要设定使用github的地址。同样的,extentreports-testng-adapter需要做同样的修订。

  • 打开extentreports-testng-adapter的github地址https://github.com/extent-framework/extentreports-testng-adapter并登陆github
  • 点击右上的fork按钮把项目fork到你的命名空间下。多说几句,在github工作模式中,提供了fork功能供非项目组成员修改源代码的方法,然后提pull request即可合并到原项目,从而省去了项目的负责人管理和维护项目成员的麻烦,同时,方便了让更多的开发者修改贡献源代码
  • 然后在本地的IDE,比如idea或者eclipse将fork出的项目克隆到本地
  • 找到com.aventstack.extentreports.service.ExtentService的第200行,我们增加使用GITHUB的CDN:
html.config().setResourceCDN(ResourceCDN.EXTENTREPORTS);
  • 为了和原版本进行区分,修改pom.xml的version为1.0.3.X
<version>1.0.3.X</version>
<distributionManagement>
 <repository>
 <id>nexus-releases</id>
 <url>
 http://192.168.10.32:8081/repository/maven-releases/
 </url>
 </repository>
 <snapshotRepository>
 <id>nexus-snapshots</id>
 <url>
 http://192.168.10.32:8081/repository/maven-snapshots/
 </url>
 </snapshotRepository>
</distributionManagement>
  • 在idea中点击deploy进行部署到私服:

与TestNG的配合使用

下面我们看看如何与TestNG配合使用。

首先修改pom增加依赖:

<dependency>
 <groupId>com.aventstack</groupId>
 <artifactId>extentreports-testng-adapter</artifactId>
 <version>1.0.3.X</version>
</dependency>

注意里面的版本是我们修改后的1.0.3.X。

接着在TestNG的class上或者suite上增加注解Listeners。Extent提供了4个listener:

ExtentITestListenerClassAdapter (ITestListener)
ExtentITestListenerAdapter (ITestListener)
ExtentIReporterSuiteClassListenerAdapter (IReporter)
ExtentIReporterSuiteListenerAdapter (IReporter)

这四种监听器的区别如下:

监听器 报告结构 生成策略

ExtentITestListenerClassAdapter Class 每执行完一个Test更新一次报告

Test

ExtentITestListenerAdapter Test 每执行完一个Test更新一次报告

ExtentIReporterSuiteClassListenerAdapter Suite 每执行完一个suite更新一次报告

Class

Test

ExtentIReporterSuiteListenerAdapter Suite 每执行完一个suite更新一次报告

Test

可能大家有些疑惑,没关系,后面我们来几个具体的示例。

然后我们在@Test注解里,给groups属性按如下格式增加Extent report的属性:

tagName: 会赋值给category

t:another-tagName, tag:another-tagName: 会赋值给category

a:authorName, author:authorName: 会赋值给作者

d:deviceName, device:deviceName: 会赋值给设备

比如我们的具体示例:

@Test(groups={"interface", "t:sanity", "a:TestExpert", "d:windows"})
public void testGet(){
try {
Unirest.get("http://www.facebook.com").asString().getBody();
} catch (Exception e) {
Assert.fail(e.getMessage(), e);
}
}
@Test(groups={"interface", "a:TestExpert001", "d:windows"})
public void testGet2(){
try {
Unirest.get("http://www.baidu.com").asString().getBody();
} catch (Exception e) {
Assert.fail(e.getMessage(), e);
}
}

并新建一个suite.xml:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="演示suite">
 <test name="演示test">
 <classes>
 <class name="com.topqa.training.web.demo.extentreport.TestUrl2" />
 </classes>
 </test>
</suite>

运行之前,我们需要在classpath里面创建extent.properties文件,用于指定生成的报告类型和位置:

extent.reporter.html.start=true
extent.reporter.html.out=test-output/HtmlReport/ExtentHtml.html

其他配置信息可以参考http://extentreports.com/docs/versions/4/java/extentservice.html

下面我们执行此TestNG的用例,完成之后即可在test-output/HtmlReport里面看到生成的报告:

测试开发 | 使用Extent report美化单元测试框架TestNG的报告

 

同时我们在左面看到根据类别、作者、缺陷进行的分类显示:

测试开发 | 使用Extent report美化单元测试框架TestNG的报告

 

很便捷,有么有。

下面我们把listener更改为ExtentITestListenerAdapter,注意看,默认的层级为@Test而不是class了:

测试开发 | 使用Extent report美化单元测试框架TestNG的报告

 

再更改为ExtentIReporterSuiteClassListenerAdapter,可以看到按suite - class - test级别进行展示:

 

最后更改为ExtentIReporterSuiteListenerAdapter,可以看到按suite - test级别进行展示:

测试开发 | 使用Extent report美化单元测试框架TestNG的报告

 

总结

extentreports-testng-adapter是Extent为TestNG提供的原生支持,能够非常方便、快捷的生成一个基于TestNG的测试报告,非常好用。作为自动化测试的编写人员和测试框架的开发者,最好掌握。

个人博客:http://www.tester.gold/

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值