前言
《Rest Assured+TestNg实现数据驱动的接口测试》一文,笔者使用ReportNg默认的报告模板,虽说自定义模板后能满足基本诉求,但是仍显得不够档次,遂想用其他优秀的report框架替换之。久闻Allure大名,特抽时间做了一番探索。
Allure介绍
Allure框架是一种灵活的轻量级多语言测试报告工具,它不仅能够以简洁的web报告形式显示已测试的内容,而且允许参与开发过程的每个人从测试的日常执行中提取最大限度的有用信息。
多语言:
- Java
- Python
- JavaScript
- Ruby
- Groovy
- PHP
- .Net
- Scala
一睹Allure风采
在展开Allure详述前,先上一份测试报告,报告主要包含总览、类别、测试套件、图表、时间刻度、功能、包等7大部分,支持自定义诸多信息,包括附件添加、缺陷链接、案例链接、测试步骤、Epic、Feature、Story、Title、案例级别等,相当强大。
Allure安装
以windows为例,其他系统的可参考官网。
- 下载
- 运行bin目录下的allure.bat
- 添加 安装路径\allure-2.7.0\bin至环境变量PATH
安装配置成功后,使用以下命令确保Allure可用。
D:\IntelliJ_IDEA_workspace\restassured>allure --version
2.7.0
案例
pom.xml配置
<?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>org.test.restassured</groupId>
<artifactId>restassured</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<aspectj.version>1.8.10</aspectj.version>
<!-- 解决mvn编译乱码问题-->
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>io.rest-assured</groupId>
<artifactId>rest-assured</artifactId>
<version>3.1.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<version>6.11</version>
</dependency>
<dependency>
<groupId>net.sourceforge.jexcelapi</groupId>
<artifactId>jxl</artifactId>
<version>2.6.12</version>
</dependency>
<!-- 依赖reportNg 关联testNg-->
<dependency>
<groupId>org.uncommons</groupId>
<artifactId>reportng</artifactId>
<version>1.1.4</version>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>ru.yandex.qatools.allure</groupId>
<artifactId>allure-testng-adaptor</artifactId>
<version>1.3.6</version>
<exclusions>
<exclusion>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
</exclusion>
</exclusions>
</dependency>
<!-- 依赖Guice -->
<dependency>
<groupId>com.google.inject</groupId>
<artifactId>guice</artifactId>
<version>4.0</version>
</dependency>
<dependency>
<groupId>io.qameta.allure</groupId>
<artifactId>allure-testng</artifactId>
<version>2.0-BETA14</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<!-- 添加插件,添加ReportNg的监听器,修改最后的TestNg的报告 -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.5</version>
<configuration>
<properties>
<property>
<name>usedefaultlisteners</name>
<value>false</value>
</property>
<property>
<name>listener</name>
<value>org.uncommons.reportng.HTMLReporter,org.uncommons.reportng.JUnitXMLReporter</value>
</property>
</properties>
<workingDirectory>target/</workingDirectory>
<forkMode>always</forkMode>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.20</version>
<configuration>
<argLine>
-javaagent:"${settings.localRepository}/org/aspectj/aspectjweaver/${aspectj.version}/aspectjweaver-${aspectj.version}.jar"
</argLine>
<!--生成allure-result的目录-->
<systemProperties>
<property>
<name>allure.results.directory</name>
<value>./target/allure-results</value>
</property>
</systemProperties>
</configuration>
<dependencies>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>${aspectj.version}</version>
</dependency>
</dependencies>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>7</source>
<target>7</target>
</configuration>
</plugin>
</plugins>
</build>
</project>
案例详述
示例代码
import com.demo.data.CasesDataProvider;
import io.qameta.allure.*;
import io.restassured.RestAssured;
import io.restassured.path.json.JsonPath;
import io.restassured.response.Response;
import org.testng.Assert;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;
import ru.yandex.qatools.allure.annotations.Title;
import java.io.IOException;
import java.net.URISyntaxException;
import java.net.URL;
import java.nio.file.Paths;
import static io.restassured.RestAssured.given;
import static java.lang.String.format;
import static java.nio.file.Files.readAllBytes;
import static org.testng.Assert.fail;
@Epic("Allure Epic")
@Feature("Allure Feature")
class RunTest {
@BeforeClass(description = "测试环境参数")
public void setUp() {
RestAssured.baseURI = "http://10.232.138.107";
RestAssured.basePath = "v1/gateway.do";
RestAssured.port = 8187;
}
@Test
@Story("failedTest")
@Description("failedTest Description")
public void failedTest(){
Assert.assertEquals(2,3);
}
@Test(dependsOnMethods = {"failedTest"})
@Story("skipTest")
@Description("skipTest Description")
public void skipTest(){
Assert.assertEquals(2,2);
}
@Story("短信发送Story")
@Description("描述发送短信接口")
@Issue("123")
@TmsLink("test-123")
@Title("Tomandydddddd")
@Severity(SeverityLevel.BLOCKER)
@Test(dataProvider = "casesProvider", dataProviderClass = CasesDataProvider.class)
public void runCases(String caseNo, String testPoit, String preResult, String YorN, String tableCheck, String appId, String merchantId, String api, String version,
String phone, String bizTransaction, String acctType) throws IOException, URISyntaxException {
String bodyString = "{\n" +
"\t\"appId\":\"" + appId + "\",\n" +
"\t\"api\":\"" + api + "\",\n" +
"\t\"data\":{\n" +
"\t\t\"merchantId\":\"" + merchantId + "\",\n" +
"\t\t\"bizTransaction\":\"" + bizTransaction + "\",\n" +
"\t\t\"phone\":\"" + phone + "\",\n" +
"\t\t\"acctType\":\"" + acctType + "\"\n" +
"\t\t},\n" +
"\t\"version\":\"" + version + "\"\n" +
"}\n";
//测试报告展现 请求报文
requestBody(RestAssured.baseURI+":"+RestAssured.port+"/"+RestAssured.basePath,bodyString);
Response response = given()
.contentType("application/json;charset=UTF-8")
.request()
.body(bodyString)
.post();
response.prettyPrint();//格式化参数
//断言
String json = response.asString();
JsonPath jp = new JsonPath(json);
//测试报告展现 请求报文
respondBody(json);
//添加附件
addAttachment();
if (response.statusCode() == 200) { //请求成功
Assert.assertEquals(jp.get("message").toString(), preResult);
} else {
Assert.assertEquals(jp.get("data.errMsg").toString(), preResult);
}
}
@Attachment(value = "附件",type = "properties")
public byte[] addAttachment() throws IOException, URISyntaxException {
return getSampleFile("allure.properties");
}
private byte[] getSampleFile(String fileName) throws URISyntaxException, IOException {
URL resource = getClass().getClassLoader().getResource(fileName);
if(resource == null){
fail(format("Couldn't find resource '%s'", fileName));
}
return readAllBytes(Paths.get(resource.toURI()));
}
@Step
public void requestBody(String URL,String Body){
//报告展现请求报文
}
@Step
public void respondBody(String Respond){
//报告展现响应报文
}
}
Allure提供了以下常用注解(未列出部分请访问官网了解),具体用法如下。
- @Epic
敏捷的术语,定义史诗,往下再分Feature和Story。 - @Feature
敏捷的术语,定义功能模块,往下是Story。 - @Story
敏捷的术语,定义用户故事。 - @Title
定义用例名称。 - @Description
定义用例描述。 -
@Issue
定义缺陷链接。可结合@Link使用,也可以结合配置文件使用。配置文件放到resource目录下,Allure 会替换{}为对应注解的值。
allure.results.directory=allure-results
allure.link.mylink.pattern=http://xxx/mylink/{}
allure.link.issue.pattern=http://xxx/issue/{}
allure.link.tms.pattern=http://xxx/tms/{}
- @TmsLink
与@Issue类似用法,定义案例链接。 - @Link
定义一个链接,在测试报告展现。 - @Severity
定义用例的级别,主要有BLOCKER,CRITICAL,MINOR,NORMAL,TRIVIAL等几种类型,默认是NORMAL。使用方法如下。
@Severity(SeverityLevel.TRIVIAL)
- @Attachment
添加已有附件或者新建附件后添加至测试报告。
//添加allure.properties附件至测试报告。
@Attachment(value = "附件",type = "properties")
public byte[] addAttachment() throws IOException, URISyntaxException {
return getSampleFile("allure.properties");
}
private byte[] getSampleFile(String fileName) throws URISyntaxException, IOException {
URL resource = getClass().getClassLoader().getResource(fileName);
if(resource == null){
fail(format("Couldn't find resource '%s'", fileName));
}
return readAllBytes(Paths.get(resource.toURI()));
}
//创建附件,附件内容为Tomandy
@Attachment
public String makeAttach() {
return "Tomandy";
}
其他
前面报告“总览”展现的“环境”和“类别”中的分类可以通过以下方式实现。
- “环境”展现:
创建environment.properties,放到target/allure-results目录下,然后再生成测试报告。
environment.properties内容:
URL=10.232.138.107
PORT=8187
-
“类别”展现:
默认的类别有Product defects (failed tests)和Test defects (broken tests)两种。可以自定义扩展,方法与第一点的“环境”类似,在生成测试报告前,将categories.json文件放到target/allure-results目录,然后再生成报告。
[{
"name": "Ignored tests",
"matchedStatuses": ["skipped"]
},
{
"name": "Infrastructure problems",
"matchedStatuses": ["broken", "failed"],
"messageRegex": ".*bye-bye.*"
},
{
"name": "Outdated tests",
"matchedStatuses": ["broken"],
"traceRegex": ".*FileNotFoundException.*"
},
{
"name": "Product defects",
"matchedStatuses": ["failed"]
},
{
"name": "Test defects",
"matchedStatuses": ["broken"]
}
]
官网对categories.json的解释如下:
name: (mandatory) category name
matchedStatuses:(optional) list of suitable test statuses. Default ["failed", "broken", "passed", "skipped", "unknown"]
messageRegex: (optional) regex pattern to check test error message. Default ".*"
traceRegex: (optional) regex pattern to check stack trace. Default ".*"
-
测试套件
通过xml方式运行案例的话,报告的“测试套件”会分类展现相应内容,如下图所示。
通过xml方式执行案例话,需在pom.xml文件加上以下内容。
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.19</version>
<configuration>
<suiteXmlFiles>
<!--该文件位于工程根目录时,直接填写名字,其它位置要加上路径-->
<suiteXmlFile>src/test/java/testNG.xml</suiteXmlFile>
</suiteXmlFiles>
</configuration>
</plugin>
报告生成命令
执行以下命令后,生成测试报告,并使用默认浏览器打开。
cd maven项目路径
mvn clean test
allure serve target/allure-results
Jenkins Allure集成
持续集成离不开Jenkins,Allure支持与Jenkins的集成,详情访问Allure官网了解。