Junit5使用allure2測試報告

Allure2 和 ExtentReports 比較

java+junit5 中常用的有 extentreports 報告模板還有 allure2 報告模板。二者都比較常用,但是 allure2 用到應該稍微多一些

extentreports 和 allure2 比較:

  • extentreports 生成的是 html 報告方便 jenkins 的郵件發送
  • extentreports 只支持 java 和 .net 比 allure2 支持範圍少很多
  • extentreports 可定製的內容展示比 allure2 少
  • 但是 extentreports 帶有幾種展示歷史報告的服務端,這個很贊
  • extentreports 官網有 V2.x 和 V3.x 版本的,V3.x 版本的只支持 jkd 8 及其以上

在這裏插入圖片描述

具體操作步驟

第一步:配置依賴

注意下方還要加上自己的 testng 或者 junit 的依賴,否則怎麼去做測試呢

也要注意下面的版本可以自己去選擇,我個人建議使用下方我的代碼,因爲 allure2 官網代碼中實際有錯誤,目前還沒改。而且改動的話 allure2 版本和 junit5 版本不對應會報錯!

下面的配置代碼都可以在 allure2 官網找到,這裏提供 allure2 官網鏈接https://docs.qameta.io/allure

<properties>
    <aspectj.version>1.8.10</aspectj.version>
</properties>

<dependencies>
    <dependency>
        <groupId>org.junit.jupiter</groupId>
        <artifactId>junit-jupiter</artifactId>
        <version>RELEASE</version>
        <scope>test</scope>
    </dependency>
    <!-- https://mvnrepository.com/artifact/io.qameta.allure/allure-junit5 -->
    <dependency>
        <groupId>io.qameta.allure</groupId>
        <artifactId>allure-junit5</artifactId>
        <version>RELEASE</version>
        <scope>test</scope>
    </dependency>
</dependencies>

<build>
    <plugins>
        <plugin>
            <artifactId>maven-surefire-plugin</artifactId>
            <version>2.22.1</version>
            <configuration>
                <testFailureIgnore>false</testFailureIgnore>
                <argLine>
                    -javaagent:"${settings.localRepository}/org/aspectj/aspectjweaver/${aspectj.version}/aspectjweaver-${aspectj.version}.jar"
                </argLine>
                <systemProperties>
                    <property>
                        <name>junit.jupiter.extensions.autodetection.enabled</name>
                        <value>true</value>
                    </property>
                </systemProperties>
            </configuration>
            <dependencies>
                <dependency>
                    <groupId>org.junit.platform</groupId>
                    <artifactId>junit-platform-surefire-provider</artifactId>
                    <version>1.2.0</version>
                </dependency>
                <dependency>
                    <groupId>org.aspectj</groupId>
                    <artifactId>aspectjweaver</artifactId>
                    <version>${aspectj.version}</version>
                </dependency>
            </dependencies>
        </plugin>
        <plugin>
            <groupId>io.qameta.allure</groupId>
            <artifactId>allure-maven</artifactId>
            <version>2.10.0</version>
            <configuration>
                <reportVersion>2.4.1</reportVersion>
            </configuration>
        </plugin>
    </plugins>
</build>

第二步:查看測試報告

運行測試用例,然後在項目中會生 allure2 的文件包,其中有測試報告,但是我們打開發現顯示的是空白,這是因爲需要你啓動 jetty 服務,所以請看下面幾步

第三步:下載 allure2 工具並配置環境變量

allure2 工具下載

下載好後,環境變量配置到 allure 的 bin 下爲止,然後 cmd 中可以allure --version查看配置是否成功

第四步:allure2 工具創建本地 jetty 服務器實例

在項目 dir 路徑下執行allure serve allure-results文件夾相對於項目的路徑,比如說執行allure serve target/allure-results,這樣就可以成功創建本地 jetty 服務器實例,這樣我們在瀏覽器可以看到很好看的 allure2 測試報告了!

allure 的使用

對於按照上面操作安裝後我們跑完測試用例會在項目下直接生成一個 allure-results 文件,若我們配置好了 allure 環境變量,我們執行下面就可以直接打開默認瀏覽器查看報告了

allure serve allure-results

對於沒有 allure-results 文件,我們也可以運行 target 下的

allure serve target/surefire-reports

生成 html 的壓縮包利於發送給別人

allure generate allure-results/ -o /tmp/allure

allure 依賴還支持在測試方法上加上 @Description 註解來表示含義還有 @Attachment 等諸多依賴可以使用,這些註解的作用都是豐富測試報告

補充說明

當你第一次裝完這些並且配置好這些工具之後,以後對於新的項目,你只用 pom 文件配置一下 allure2,然後運行測試用例讓其在 target 下產生報告,再運行allure serve target/allure-results創建 jetty 實例就行了,這樣就可以看報告了

另外下面這裏在提供 allure2 的官網:

  • allure2 工具官網介紹安裝

    注意,這裏是 allure2 工具不是那個依賴,用來產生 jetty 實例的。官網 allure2 安裝,官網的這個安裝方式通過 Scoop 工具去安裝,我不推薦,這種方式直接給弄麻煩了,我們可以直接去 github 一搜然後 download 一下,再配一個環境變量就可以了

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章