Allure生成測試報告

  • 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配置文件生效


在這里插入圖片描述
最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市,隨后出現的幾起案子,更是在濱河造成了極大的恐慌,老刑警劉巖,帶你破解...
    沈念sama閱讀 227,702評論 6 531
  • 序言:濱河連續發生了三起死亡事件,死亡現場離奇詭異,居然都是意外死亡,警方通過查閱死者的電腦和手機,發現死者居然都...
    沈念sama閱讀 98,143評論 3 415
  • 文/潘曉璐 我一進店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人,你說我怎么就攤上這事。” “怎么了?”我有些...
    開封第一講書人閱讀 175,553評論 0 373
  • 文/不壞的土叔 我叫張陵,是天一觀的道長。 經常有香客問我,道長,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 62,620評論 1 307
  • 正文 為了忘掉前任,我火速辦了婚禮,結果婚禮上,老公的妹妹穿的比我還像新娘。我一直安慰自己,他們只是感情好,可當我...
    茶點故事閱讀 71,416評論 6 405
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著,像睡著了一般。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發上,一...
    開封第一講書人閱讀 54,940評論 1 321
  • 那天,我揣著相機與錄音,去河邊找鬼。 笑死,一個胖子當著我的面吹牛,可吹牛的內容都是我干的。 我是一名探鬼主播,決...
    沈念sama閱讀 43,024評論 3 440
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了?” 一聲冷哼從身側響起,我...
    開封第一講書人閱讀 42,170評論 0 287
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后,有當地人在樹林里發現了一具尸體,經...
    沈念sama閱讀 48,709評論 1 333
  • 正文 獨居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內容為張勛視角 年9月15日...
    茶點故事閱讀 40,597評論 3 354
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發現自己被綠了。 大學時的朋友給我發了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 42,784評論 1 369
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖,靈堂內的尸體忽然破棺而出,到底是詐尸還是另有隱情,我是刑警寧澤,帶...
    沈念sama閱讀 38,291評論 5 357
  • 正文 年R本政府宣布,位于F島的核電站,受9級特大地震影響,放射性物質發生泄漏。R本人自食惡果不足惜,卻給世界環境...
    茶點故事閱讀 44,029評論 3 347
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧,春花似錦、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 34,407評論 0 25
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至,卻和暖如春,著一層夾襖步出監牢的瞬間,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 35,663評論 1 280
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人。 一個月前我還...
    沈念sama閱讀 51,403評論 3 390
  • 正文 我出身青樓,卻偏偏與公主長得像,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當晚...
    茶點故事閱讀 47,746評論 2 370

推薦閱讀更多精彩內容