基於Java+Maven+Testng+Selenium+Log4j+Allure+Jenkins搭建一個WebUI自動化框架(4)集成Allure報表


通過集成Allure報表,可以讓自動化測試結果以美觀的圖形化界面展現出來。集成步驟:

1、在pom.xml文件中添加allure依賴

<!--allure報表依賴-->
<dependency>
    <groupId>io.qameta.allure</groupId>
    <artifactId>allure-testng</artifactId>
    <version>2.12.1</version>
    <scope>test</scope>
</dependency>

2、在pom.xml文件中的<project>標簽下設置屬性,避免亂碼

<properties>
    <aspectj.version>1.8.10</aspectj.version>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
    <maven.compiler.encoding>UTF-8</maven.compiler.encoding>
</properties>

3、在<project>標簽下引入Maven Surefire插件:生成Allure報表

<build>
    <plugins>
        <plugin>
            <!-- maven-surefire-plugin 配合testng執行測試用例的maven插件 -->
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-surefire-plugin</artifactId>
            <version>2.22.1</version>
            <configuration>
                <!-- 測試失敗后,是否忽略並繼續測試 -->
                <testFailureIgnore>true</testFailureIgnore>
                <suiteXmlFiles>
                    <!-- testng配置文件名稱 -->
                    <suiteXmlFile>testng.xml</suiteXmlFile>
                </suiteXmlFiles>
                <!--設置參數命令行 -->
                <argLine>
                    <!-- UTF-8編碼 -->
                    -Dfile.encoding=UTF-8
                    <!-- 配置攔截器 -->
                    -javaagent:"${settings.localRepository}/org/aspectj/aspectjweaver/${aspectj.version}/aspectjweaver-${aspectj.version}.jar"
                </argLine>
                <systemProperties>
                    <property>
                        <!-- 配置 allure 結果存儲路徑 -->
                        <name>allure.results.directory</name>
                        <value>${project.build.directory}/allure-results</value>
                    </property>
                </systemProperties>
            </configuration>
            <dependencies>
                <!-- aspectjweaver maven坐標 -->
                <dependency>
                    <groupId>org.aspectj</groupId>
                    <artifactId>aspectjweaver</artifactId>
                    <version>${aspectj.version}</version>
                </dependency>
            </dependencies>
        </plugin>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <configuration>
                <source>8</source>
                <target>8</target>
            </configuration>
        </plugin>
    </plugins>
</build>

此步驟如果遇到<build>標簽下某個包依賴解決不了,可以將這個包放到<project>標簽下的<dependencies>標簽后,再引入到<build>標簽中

 

至此,Allure報表的集成操作已經完成了,接下來就可以使用Allure報表生成測試報告。

通過Allure報表生成報告的操作:

(1)在工程目錄下新建個testng.xml文件,此處的文件需要與上述Maven Surefire插件配置的testng.xml文件名一致,填入如下信息:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="All Test Suite" parallel="tests" thread-count="2">
    <test name="測試">
        <classes>
            <class name="com.lrc.testcases.TestBaidu3"/>
        </classes>
    </test>
</suite>

其中的class是測試用例的類名,文件放置的目錄如下圖:

(2)在命令行執行命令:

mvn clean test

注意:必須使用maven構建測試執行,不能直接在測試類中執行或者在testng.xml中右鍵執行,那樣是生成不了allure報表的。

輸入完命令后回車,就會開始構建執行測試用例:

(3)生成allure報表:

輸入完后,就會生成了Allure報表:

 

可以清楚查看我們每條用例的執行情況:


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM