- Allure是一個(gè)輕量級(jí),靈活的鲸阔,支持多語(yǔ)言的測(cè)試報(bào)告工具
- 多平臺(tái)的订晌,奢華的測(cè)試報(bào)告框架
- 可以為dev/qa提供詳盡的測(cè)試報(bào)告,測(cè)試步驟饿自,log
- 也可以為管理層提供high level的統(tǒng)計(jì)報(bào)告
- java語(yǔ)言開(kāi)發(fā)的汰翠,支持pytest,js昭雌,php复唤,ruby等
- 可以集成到Jenkins
一,下載對(duì)應(yīng)的包&配置環(huán)境變量
1.下載allure
https://repo.maven.apache.org/maven2/io/qameta/allure/allure-commandline/2.13.2/
ps:這里注意烛卧,不要下2.13.10版本佛纫,這個(gè)打開(kāi)會(huì)空白!W芊拧呈宇!
2. 配置環(huán)境變量:
- jdk1.8版本及以上
- 在path那邊添加環(huán)境變量,比如D:\java課程\allure-commandline-2.13.2\allure-2.13.2\bin局雄,配置完成后可以cmd輸入allure --version驗(yàn)證是否配置成功
二. 添加依賴
這里匹配的是.Test結(jié)尾的java文件
<?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>com.hogwarts</groupId>
<artifactId>AllureDemo2</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
<version>RELEASE</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.hamcrest</groupId>
<artifactId>hamcrest</artifactId>
<version>2.2</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.qameta.allure</groupId>
<artifactId>allure-junit5</artifactId>
<version>2.13.6</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.platform</groupId>
<artifactId>junit-platform-console-standalone</artifactId>
<version>1.7.0</version>
</dependency>
<dependency>
<groupId>org.hamcrest</groupId>
<artifactId>hamcrest</artifactId>
<version>2.1</version>
<scope>compile</scope>
</dependency>
</dependencies>
<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
<aspectj.version>1.8.10</aspectj.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<build>
<plugins>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>3.0.0-M5</version>
<configuration>
<includes>
<include>**/*Test.java</include>
</includes>
</configuration>
</plugin>
</plugins>
</build>
</project>
allure常用命令
- allure --help 幫助
- allure --version 查看版本信息
- allure serve 生成在線版本的測(cè)試報(bào)告甥啄,可以通過(guò)這個(gè)來(lái)解析最終的xUnit生成的結(jié)果
- allure generate <allure-result中間文件> -o 輸出目錄(默認(rèn)路徑:allure report)
三,編寫(xiě)測(cè)試case并運(yùn)行
在test/java下新建測(cè)試case
import org.junit.jupiter.api.Test;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.equalTo;
public class Demo1Test {
@Test
public void demo1() {
assert 1 + 1 == 2;
}
@Test
public void demo2() {
assertThat(2, equalTo(2));
}
@Test
public void demo3() {
assertThat(2, equalTo(2));
}
}
四. 使用allure工具解析測(cè)試報(bào)告
然后點(diǎn)擊pom文件 - 右鍵 - open in terminal - 輸入mvn clean test命令清空target目錄下的surefile-reports
運(yùn)行完上面的命令后哎榴,因?yàn)槲覀冊(cè)趐om文件配置了surfile型豁,使用我們?cè)诿钚休斎隺llure serve ./target/surefile-reports生成報(bào)告,但是這個(gè)我這測(cè)試時(shí)候不行尚蝌,只能用后面的allure serve allure-results(這個(gè)想要是最新的需要手動(dòng)刪除allure-results目錄),會(huì)自動(dòng)跳轉(zhuǎn)網(wǎng)頁(yè)查看報(bào)告
如果只想執(zhí)行單個(gè)case迎变,直接輸入
mvn clean -Dtest=com.hogwarts.demo.Demo2Test test
常用注解
演示幾個(gè)常用注解:
- 用例名稱和描述展示
import io.qameta.allure.Allure;
import io.qameta.allure.Description;
import org.junit.Assert;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
/**
* @DisplayName 在外層顯示case名稱
* @Description:對(duì)case的具體描述,在詳情里展示飘言,是靜態(tài)描述信息
* Allure.description:測(cè)試動(dòng)態(tài)描述信息
*/
public class AllureDisplayAndDescriptionTest {
@Test
@DisplayName("測(cè)試顯示名稱")
@Description("這是一個(gè)case的靜態(tài)描述信息")
void test1(){
Assert.assertEquals(1,1);
}
@Test
@DisplayName("測(cè)試動(dòng)態(tài)描述信息")
void test2(){
Assert.assertEquals(1,1);
Allure.description("11111");
Assert.assertEquals(1,1);
// 動(dòng)態(tài)描述信息(寫(xiě)多個(gè)的時(shí)候只會(huì)展示最后一次添加的
Allure.description("22222");
}
}
展示如下
- 添加鏈接
import io.qameta.allure.Allure;
import io.qameta.allure.Link;
import io.qameta.allure.Links;
import org.junit.jupiter.api.Test;
/**
* 添加鏈接
* 2種:
* 內(nèi)部鏈接:@Link
* 外部鏈接:Allure.link("跳轉(zhuǎn)到百度","https://www.baidu.com");
*/
public class AllureLinkTest {
@Test
@Link(name = "跳轉(zhuǎn)到百度",url = "https://www.baidu.com")
void link1(){
System.out.println("外部鏈接");
}
@Test
void link2(){
System.out.println("內(nèi)部鏈接");
Allure.link("跳轉(zhuǎn)到百度","https://www.baidu.com");
}
// 添加多個(gè)鏈接
@Test
@Links({
@Link(name = "link 1",url = "https://www.baidu.com"),
@Link(name = "link 2",url = "https://www.ceshiren.com"),
})
void multiLinks(){
System.out.println("添加多個(gè)鏈接");
}
}
展示如下:
-
用例等級(jí)
在這里插入圖片描述
import io.qameta.allure.Severity;
import io.qameta.allure.SeverityLevel;
import org.junit.jupiter.api.Test;
/**
* 等級(jí)劃分執(zhí)行順序:
* 依次是blocker>critical>normal>minor>trivial
*/
public class AllureLevelTest {
@Test
@Severity(SeverityLevel.BLOCKER)
void blockerDemo(){
System.out.println("BLOCKER");
}
@Test
@Severity(SeverityLevel.CRITICAL)
void criticalDemo(){
System.out.println("CRITICAL");
}
@Test
@Severity(SeverityLevel.MINOR)
void minorDemo(){
System.out.println("MINOR");
}
@Test
@Severity(SeverityLevel.NORMAL)
void normalDemo(){
System.out.println("NORMAL");
}
@Test
@Severity(SeverityLevel.TRIVIAL)
void trivialDemo(){
System.out.println("TRIVIAL");
}
}
- feature和sotry
import io.qameta.allure.Feature;
import io.qameta.allure.Story;
import org.junit.jupiter.api.Test;
/**
* @Feature:大類
* @Story:大類下面的小類
*/
@Feature("登錄")
public class AllureFeatureTest {
@Test
@Story("登錄成功")
void loginSuccess(){
System.out.println("登錄成功");
}
@Test
@Story("登錄失敗")
void testFail1(){
System.out.println("fail1");
}
@Test
@Story("登錄失敗")
void testFail2(){
System.out.println("fail2");
}
}
在首頁(yè)會(huì)做具體展示衣形,點(diǎn)進(jìn)去可查看詳情
- 添加測(cè)試步驟
import io.qameta.allure.Allure;
import io.qameta.allure.Step;
import org.junit.Assert;
import org.junit.jupiter.api.Test;
public class AllureStepTest {
@Test
public void stepTest() {
Allure.step("step1", this::step1);
Allure.step("step2", this::step2);
Allure.step("step2", this::step3);
}
@Step("step1")
void step1() {
System.out.println("step1");
}
@Step("step2")
void step2() {
System.out.println("step2");
}
@Step("step3")
void step3() {
Assert.assertEquals(1, 2);
}
}
在頁(yè)面展示如下:如果成功會(huì)顯示√標(biāo)識(shí),失敗會(huì)標(biāo)紅并且顯示報(bào)錯(cuò)
- 添加附件:支持添加文本/圖片/視頻等
import io.qameta.allure.Allure;
import org.junit.jupiter.api.Test;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
public class AllureAddAttachmentTest {
@Test
void addTextAndPics(){
Allure.addAttachment("添加文本","我是文本內(nèi)容");
try {
Allure.addAttachment("添加圖片","image/png", new FileInputStream("C:\\Users\\86189\\Pictures\\clipboard1.png"),"png");
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
}
測(cè)試報(bào)告中文展示亂碼問(wèn)題解決
在pom文件添加properties配置文件
<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
<aspectj.version>1.8.10</aspectj.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
我這里也改了,但是我感覺(jué)改不改關(guān)系不大谆吴,主要還是properties配置文件生效