网站首页 > 开源技术 正文
引言
上一节分享Extent Reports,这次分享一个与之齐名的Allure。本篇只是翻译国外一大神的文章。这位大神前一篇也分享了Extent Reports,这里只翻译第二篇。所以你的看到代码里包括两个报告框架。Allure是Yandex QA Team团队开发的。
依赖
想要集成Allure,需要在pom.xml里添加以下依赖。
Properties Section
<properties>
<aspectj.version>1.8.10</aspectj.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
Allure TestNG Dependency
<dependency>
<groupId>io.qameta.allure</groupId>
<artifactId>allure-testng</artifactId>
<version>2.12.0</version>
</dependency>
Build Section
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.20</version>
<configuration>
<suiteXmlFiles>
<suiteXmlFile>TestNG.xml</suiteXmlFile>
</suiteXmlFiles>
<argLine>
-javaagent:"${settings.localRepository}/org/aspectj/aspectjweaver/${aspectj.version}/aspectjweaver-${aspectj.version}.jar"
</argLine>
</configuration>
<dependencies>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>${aspectj.version}</version>
</dependency>
</dependencies>
</plugin>
</plugins>
</build>
搞定,这就是所有pom.xml里要添加的东西。最终的pom是这个样子:(TestNG建议改成最新版本,不然会报错。)
?xml version="1.0" encoding="UTF-8"?>
<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>allurereportexample</groupId>
<artifactId>allurereportexample</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<aspectj.version>1.8.13</aspectj.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>3.141.59</version>
</dependency>
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<version>6.14.3</version>
</dependency>
<dependency>
<groupId>ru.yandex.qatools.ashot</groupId>
<artifactId>ashot</artifactId>
<version>1.5.4</version>
</dependency>
<dependency>
<groupId>io.qameta.allure</groupId>
<artifactId>allure-testng</artifactId>
<version>2.12.0</version>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
<dependency>
<groupId>com.relevantcodes</groupId>
<artifactId>extentreports</artifactId>
<version>2.41.2</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.20</version>
<configuration>
<suiteXmlFiles>
<suiteXmlFile>TestNG.xml</suiteXmlFile>
</suiteXmlFiles>
<argLine>
-javaagent:"${settings.localRepository}/org/aspectj/aspectjweaver/${aspectj.version}/aspectjweaver-${aspectj.version}.jar"
</argLine>
</configuration>
<dependencies>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>${aspectj.version}</version>
</dependency>
</dependencies>
</plugin>
</plugins>
</build>
</project>
配置Allure Features并修改测试代码
接下来我会基于下面的图片,逐个讲解每个Feature。我对测试代码做了一些修改,让生成的HTML报告更易懂,展示更多有用信息。
Feature-1: 显示名称
为了让测试报告更容易理解, 这里用了@test注解的description属性。这样通常是用例的主题。
@Test (priority = 0, description=”Invalid Login Scenario with wrong username and password.”)
然后,你也可以用 @Description 注解添加更多描述信息:
@Description(“Test Description: Login test with wrong username and wrong password.”)
在测试报告中会展示成如下这个样子:
Feature-2: Steps
Step可以理解为测试步骤,它们可以被任何测试用例调用,所以在项目中要放一个公共的地方。用@Step 注解标示它是一个测试步骤。在示例项目中,我们把Step统一放在Page中。 LogintoN11, verifyLoginUserName, verifyLoginPassword都是Step。你可以看到下面的示例代码的方法上方都加了@Step注解。我们还可以用{}传参。比如:第一个方法,{0} 是第一个参数 – username, {1} 是第二个参数 – password.
LoginPage Steps:
HomePage Steps:
在测试报告中的样子如下:
Feature-3: Attachments
我们可以用@Attachment注解在报告中添加附件。该方法可以返回String, byte []等。比如:我们可以在测试报告中添加截图,该方法要返回byte []。别忘记了,在类上面加上@Listeners({ TestListener.class })声明。
在TestListener类中添加了两个附件,一个是txt,另外一个是截图。
当用例失败时就会调用这两个方法。
在测试报告中会是以下这个样子:
Feature-4: Links
你可以用@Link注解集成缺陷管理系统或者用例管理系统。
import io.qameta.allure.Link;
import io.qameta.allure.Issue;
import io.qameta.allure.TmsLink;
@Link("https://example.org")
@Link(name = "allure", type = "mylink")
public void testSomething() {
...
}
@Issue("123")
@Issue("432")
public void testSomething() {
...
}
@TmsLink("test-1")
@TmsLink("test-2")
public void testSomething() {
...
}
property文件中配置的格式如下:allure.link.my-link-type.pattern=https://example.org/custom/{}/path, allure会自动用注释上的值替换{}。
allure.link.mylink.pattern=https://example.org/mylink/{}
allure.link.issue.pattern=https://example.org/issue/{}
allure.link.tms.pattern=https://example.org/tms/{}
在示例项目中没有使用link这个功能,所以测试报告中没有。
Feature-5: Severity
我们可以用@Severity注解指定严重级别。
测试报告中是这个样子:
Feature-6: BDD报告 (Features and Stories)
喜欢BDD或者敏捷的同学,可以使用@Epic, @Feature和@Stories注解,话不多说看了代码和报告就明白了。
报告中的样子:
先介绍到这里,接下来我们执行一下,看下报告。
代码:https://github.com/swtestacademy/TestNGAllureReport
Step-3: 执行,生成Allure报告
你可以用Maven命令(mvn clean test)或者直接在IDEA执行。
IDEA中点击configurations
选择maven,在红色箭头处输入clean test,点OK。
点绿色的图标执行。
是时候生成测试报告了。
为了生成报告要安装Allure command-line解释器。
1.下载最新的zip包https://bintray.com/qameta/generic/allure2/2.7.0#files/io%2Fqameta%2Fallure%2Fallure%2F2.7.0
2. 点Files,下载zip包。zip是windows用的。如果你是mac的话可以用brew install 安装。
3.解压
4.打开bin目录
5.设置环境变量
Mac用户直接用下面命令:
brew install allure
最后,打开命令行工具,导航到项目目录,执行以上命令:
allure serve allure-results
然后,你将看到漂亮的Allure报告.
Dashboard
Categories
Suites
Graphs
Timeline
Behaviors
Packages
猜你喜欢
- 2024-10-05 TestNG7.3.0版本,运行时DTD不安全报错问题
- 2024-10-05 测试开发 | 使用Extent report美化单元测试框架TestNG的报告
- 2024-10-05 Jenkins + TestNG 实现自助式自动化测试平台
- 2024-10-05 TestNg 失败自动重试(test connection错误怎么办)
- 2024-10-05 干货!详解自动化测试用例设计与管理的通用策略(基于TestNg)
- 2024-10-05 Java老矣,Java正年轻!一文了解Java接口自动化测试框架TestNg
- 2024-10-05 十分钟上手TestNg(十分钟上手辩论)
- 2024-10-05 资深测试必备技能!TestNG自动化测试框架实战详解
- 2024-10-05 利器 | TestNG 与 Junit 对比,测试框架如何选择?
- 2024-06-27 HttpClient +Testng进行接口测试入门
你 发表评论:
欢迎- 05-15.net core集成vue
- 05-15Vue开发环境搭建
- 05-15创建Electron工程
- 05-15个人博客搭建保姆级教程3——hexo
- 05-15(实用派)Express之创建并配置运行vue项目
- 05-15package.json 与 package-lock.json 的关系
- 05-15deepin-linux的vue学习(二)git克隆开源vue项目和本地运行
- 05-15手把手教你搭建免费个人博客网站
- 最近发表
- 标签列表
-
- jdk (81)
- putty (66)
- rufus (78)
- 内网穿透 (89)
- okhttp (70)
- powertoys (74)
- windowsterminal (81)
- netcat (65)
- ghostscript (65)
- veracrypt (65)
- asp.netcore (70)
- wrk (67)
- aspose.words (80)
- itk (80)
- ajaxfileupload.js (66)
- sqlhelper (67)
- express.js (67)
- phpmailer (67)
- xjar (70)
- redisclient (78)
- wakeonlan (66)
- tinygo (85)
- startbbs (72)
- webftp (82)
- vsvim (79)
本文暂时没有评论,来添加一个吧(●'◡'●)