- Allure是一個輕量級,靈活的,支持多語言的測試報告工具
- 多平臺的,奢華的測試報告框架
- 可以為dev/qa提供詳盡的測試報告,測試步驟,log
- 也可以為管理層提供high level的統計報告
- java語言開發的,支持pytest,js,php,ruby等
- 可以集成到Jenkins
一,下載對應的包&配置環境變量
1.下載allure
https://repo.maven.apache.org/maven2/io/qameta/allure/allure-commandline/2.13.2/
ps:這里注意,不要下2.13.10版本,這個打開會空白!!!
2. 配置環境變量:
- jdk1.8版本及以上
- 在path那邊添加環境變量,比如D:\java課程\allure-commandline-2.13.2\allure-2.13.2\bin,配置完成后可以cmd輸入allure --version驗證是否配置成功
二. 添加依賴
這里匹配的是.Test結尾的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 生成在線版本的測試報告,可以通過這個來解析最終的xUnit生成的結果
- allure generate <allure-result中間文件> -o 輸出目錄(默認路徑:allure report)
三,編寫測試case并運行
在test/java下新建測試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工具解析測試報告
然后點擊pom文件 - 右鍵 - open in terminal - 輸入mvn clean test命令清空target目錄下的surefile-reports
在這里插入圖片描述
運行完上面的命令后,因為我們在pom文件配置了surfile,使用我們在命令行輸入allure serve ./target/surefile-reports生成報告,但是這個我這測試時候不行,只能用后面的allure serve allure-results(這個想要是最新的需要手動刪除allure-results目錄),會自動跳轉網頁查看報告
如果只想執行單個case,直接輸入
mvn clean -Dtest=com.hogwarts.demo.Demo2Test test
常用注解
在這里插入圖片描述
演示幾個常用注解:
- 用例名稱和描述展示
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:對case的具體描述,在詳情里展示,是靜態描述信息
* Allure.description:測試動態描述信息
*/
public class AllureDisplayAndDescriptionTest {
@Test
@DisplayName("測試顯示名稱")
@Description("這是一個case的靜態描述信息")
void test1(){
Assert.assertEquals(1,1);
}
@Test
@DisplayName("測試動態描述信息")
void test2(){
Assert.assertEquals(1,1);
Allure.description("11111");
Assert.assertEquals(1,1);
// 動態描述信息(寫多個的時候只會展示最后一次添加的
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種:
* 內部鏈接:@Link
* 外部鏈接:Allure.link("跳轉到百度","https://www.baidu.com");
*/
public class AllureLinkTest {
@Test
@Link(name = "跳轉到百度",url = "https://www.baidu.com")
void link1(){
System.out.println("外部鏈接");
}
@Test
void link2(){
System.out.println("內部鏈接");
Allure.link("跳轉到百度","https://www.baidu.com");
}
// 添加多個鏈接
@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("添加多個鏈接");
}
}
展示如下:
在這里插入圖片描述
-
用例等級
在這里插入圖片描述
import io.qameta.allure.Severity;
import io.qameta.allure.SeverityLevel;
import org.junit.jupiter.api.Test;
/**
* 等級劃分執行順序:
* 依次是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");
}
}
在首頁會做具體展示,點進去可查看詳情
在這里插入圖片描述
- 添加測試步驟
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);
}
}
在頁面展示如下:如果成功會顯示√標識,失敗會標紅并且顯示報錯
在這里插入圖片描述
- 添加附件:支持添加文本/圖片/視頻等
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("添加文本","我是文本內容");
try {
Allure.addAttachment("添加圖片","image/png", new FileInputStream("C:\\Users\\86189\\Pictures\\clipboard1.png"),"png");
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
}
在這里插入圖片描述
測試報告中文展示亂碼問題解決
在這里插入圖片描述
在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>
我這里也改了,但是我感覺改不改關系不大,主要還是properties配置文件生效
在這里插入圖片描述