【TestNG快板說八】TestNG使用Allure生成測試報告

TestNG如何生成測試報告

方式一:本地通過IDE進行測試

  1. 配置maven依賴
		<dependency>
            <groupId>io.qameta.allure</groupId>
            <artifactId>allure-testng</artifactId>
            <version>2.12.1</version>
        </dependency>

allure用到surefire查找測試用例,所以這裏還要在<plugins>中引用該插件

		<plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <version>2.20</version>
                <configuration>
                    <systemPropertyVariables>
                        <allure.results.directory>${project.build.directory}/allure-results/${maven.build.timestamp}</allure.results.directory>
                        <allure.link.issue.pattern>https://example.org/browse/{}</allure.link.issue.pattern>
                        <allure.link.tms.pattern>https://example.org/browse/{}</allure.link.tms.pattern>
                    </systemPropertyVariables>
                    <argLine>
                        -javaagent:"${settings.localRepository}/org/aspectj/aspectjweaver/${aspectj.version}/aspectjweaver-${aspectj.version}.jar"
                    </argLine>
                </configuration>
                <dependencies>
                    <dependency>
                        <groupId>org.aspectj</groupId>
                        <artifactId>aspectjweaver</artifactId>
                        <version>${aspectj.version}</version>
                    </dependency>
                </dependencies>
            </plugin>

這個插件本省依賴aspectj做AOP,所以我們要在dependencies中添加對應的依賴包,版本號用<properties>做管理

    <properties>
        <aspectj.version>1.8.10</aspectj.version>
        <maven.build.timestamp.format>yyyyMMddHHmmss</maven.build.timestamp.format>
    </properties>

上面還使用一個maven.build.timestamp.format,主要是用來每次run test之後可以在allure-result下生成一個以時間戳命名的結果文件夾,用來區分每次的運行結果,我們來看看它是在哪裏被使用:

               <configuration>
                    <systemPropertyVariables>
                        <allure.results.directory>${project.build.directory}/allure-results/${maven.build.timestamp}</allure.results.directory>
                        <allure.link.issue.pattern>https://example.org/browse/{}</allure.link.issue.pattern>
                        <allure.link.tms.pattern>https://example.org/browse/{}</allure.link.tms.pattern>
                    </systemPropertyVariables>
                    <argLine>
                        -javaagent:"${settings.localRepository}/org/aspectj/aspectjweaver/${aspectj.version}/aspectjweaver-${aspectj.version}.jar"
                    </argLine>
                </configuration>

其實就在surefire插件的配置裏,我們看到系統屬性設置systemPropertyVariables下有一個allure.results.directory,這個屬性設置了allure結果文件放置的路徑,後面就使用了${maven.build.timestamp}變量獲取build的時間戳,每次運行的時候都會生成一個對應當前時間戳放置結果。然後使用allure的命令就可以查看html格式的報告:
到對應的目錄下:

allure serve target/allure-results/20190813053653 

結果:
在這裏插入圖片描述
在這裏插入圖片描述

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