前言
《Rest Assured+TestNg實(shí)現(xiàn)數(shù)據(jù)驅(qū)動(dòng)的接口測(cè)試》一文纺座,筆者使用ReportNg默認(rèn)的報(bào)告模板息拜,雖說自定義模板后能滿足基本訴求,但是仍顯得不夠檔次净响,遂想用其他優(yōu)秀的report框架替換之。久聞Allure大名喳瓣,特抽時(shí)間做了一番探索馋贤。
Allure介紹
Allure框架是一種靈活的輕量級(jí)多語言測(cè)試報(bào)告工具,它不僅能夠以簡潔的web報(bào)告形式顯示已測(cè)試的內(nèi)容畏陕,而且允許參與開發(fā)過程的每個(gè)人從測(cè)試的日常執(zhí)行中提取最大限度的有用信息配乓。
多語言:
- Java
- Python
- JavaScript
- Ruby
- Groovy
- PHP
- .Net
- Scala
一睹Allure風(fēng)采
在展開Allure詳述前,先上一份測(cè)試報(bào)告惠毁,報(bào)告主要包含總覽犹芹、類別、測(cè)試套件鞠绰、圖表腰埂、時(shí)間刻度、功能蜈膨、包等7大部分屿笼,支持自定義諸多信息,包括附件添加翁巍、缺陷鏈接驴一、案例鏈接、測(cè)試步驟灶壶、Epic肝断、Feature、Story、Title胸懈、案例級(jí)別等担扑,相當(dāng)強(qiáng)大。
Allure安裝
以windows為例箫荡,其他系統(tǒng)的可參考官網(wǎng)魁亦。
- 下載
- 運(yùn)行bin目錄下的allure.bat
- 添加 安裝路徑\allure-2.7.0\bin至環(huán)境變量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 關(guān)聯(lián)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的監(jiān)聽器洁奈,修改最后的TestNg的報(bào)告 -->
<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 = "測(cè)試環(huán)境參數(shù)")
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("短信發(fā)送Story")
@Description("描述發(fā)送短信接口")
@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";
//測(cè)試報(bào)告展現(xiàn) 請(qǐng)求報(bào)文
requestBody(RestAssured.baseURI+":"+RestAssured.port+"/"+RestAssured.basePath,bodyString);
Response response = given()
.contentType("application/json;charset=UTF-8")
.request()
.body(bodyString)
.post();
response.prettyPrint();//格式化參數(shù)
//斷言
String json = response.asString();
JsonPath jp = new JsonPath(json);
//測(cè)試報(bào)告展現(xiàn) 請(qǐng)求報(bào)文
respondBody(json);
//添加附件
addAttachment();
if (response.statusCode() == 200) { //請(qǐng)求成功
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){
//報(bào)告展現(xiàn)請(qǐng)求報(bào)文
}
@Step
public void respondBody(String Respond){
//報(bào)告展現(xiàn)響應(yīng)報(bào)文
}
}
Allure提供了以下常用注解(未列出部分請(qǐng)?jiān)L問官網(wǎng)了解),具體用法如下绞灼。
- @Epic
敏捷的術(shù)語利术,定義史詩,往下再分Feature和Story低矮。 - @Feature
敏捷的術(shù)語印叁,定義功能模塊,往下是Story军掂。 - @Story
敏捷的術(shù)語轮蜕,定義用戶故事。 - @Title
定義用例名稱蝗锥。 - @Description
定義用例描述跃洛。 -
@Issue
定義缺陷鏈接≈找椋可結(jié)合@Link使用汇竭,也可以結(jié)合配置文件使用。配置文件放到resource目錄下穴张,Allure 會(huì)替換{}為對(duì)應(yīng)注解的值细燎。
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
定義一個(gè)鏈接玻驻,在測(cè)試報(bào)告展現(xiàn)。 - @Severity
定義用例的級(jí)別叮贩,主要有BLOCKER,CRITICAL,MINOR,NORMAL,TRIVIAL等幾種類型击狮,默認(rèn)是NORMAL。使用方法如下益老。
@Severity(SeverityLevel.TRIVIAL)
- @Attachment
添加已有附件或者新建附件后添加至測(cè)試報(bào)告彪蓬。
//添加allure.properties附件至測(cè)試報(bào)告。
@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()));
}
//創(chuàng)建附件捺萌,附件內(nèi)容為Tomandy
@Attachment
public String makeAttach() {
return "Tomandy";
}
其他
前面報(bào)告“總覽”展現(xiàn)的“環(huán)境”和“類別”中的分類可以通過以下方式實(shí)現(xiàn)档冬。
- “環(huán)境”展現(xiàn):
創(chuàng)建environment.properties,放到target/allure-results目錄下,然后再生成測(cè)試報(bào)告酷誓。
environment.properties內(nèi)容:
URL=10.232.138.107
PORT=8187
-
“類別”展現(xiàn):
默認(rèn)的類別有Product defects (failed tests)和Test defects (broken tests)兩種披坏。可以自定義擴(kuò)展盐数,方法與第一點(diǎn)的“環(huán)境”類似棒拂,在生成測(cè)試報(bào)告前,將categories.json文件放到target/allure-results目錄玫氢,然后再生成報(bào)告帚屉。
[{
"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"]
}
]
官網(wǎng)對(duì)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 ".*"
-
測(cè)試套件
通過xml方式運(yùn)行案例的話,報(bào)告的“測(cè)試套件”會(huì)分類展現(xiàn)相應(yīng)內(nèi)容漾峡,如下圖所示攻旦。
通過xml方式執(zhí)行案例話,需在pom.xml文件加上以下內(nèi)容生逸。
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.19</version>
<configuration>
<suiteXmlFiles>
<!--該文件位于工程根目錄時(shí)牢屋,直接填寫名字,其它位置要加上路徑-->
<suiteXmlFile>src/test/java/testNG.xml</suiteXmlFile>
</suiteXmlFiles>
</configuration>
</plugin>
報(bào)告生成命令
執(zhí)行以下命令后槽袄,生成測(cè)試報(bào)告烙无,并使用默認(rèn)瀏覽器打開。
cd maven項(xiàng)目路徑
mvn clean test
allure serve target/allure-results
Jenkins Allure集成
持續(xù)集成離不開Jenkins遍尺,Allure支持與Jenkins的集成皱炉,詳情訪問Allure官網(wǎng)了解。