Java+Maven+TestNG接口(API)自動化測試教程(九) 使用 Extent Reporters 美化測試報告

TestNG 自己生成的測試報告不夠美觀,我們可以使用 Extent Reporters 來美化測試報告。

9.1 在 pom.xml 中加入支持 extent reporters 的 XML 片段

增加內容後的完整的 pom.xml 文件內容如下:

<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.mytest</groupId>
    <artifactId>apidemo</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <dependencies>
        <dependency>
            <groupId>org.testng</groupId>
            <artifactId>testng</artifactId>
            <version>7.1.0</version>
        </dependency>
        <dependency>
            <groupId>org.apache.httpcomponents</groupId>
            <artifactId>httpclient</artifactId>
            <version>4.5.11</version>
        </dependency>
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>fastjson</artifactId>
            <version>1.2.62</version>
        </dependency>
        <dependency>
            <groupId>com.aventstack</groupId>
            <artifactId>extentreports</artifactId>
            <version>3.1.5</version>
        </dependency>
    </dependencies>
    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <version>3.0.0-M4</version>
                <configuration>
                    <suiteXmlFiles>
                        <!--此處testng.xml即爲要運行的testng.xml文件 -->
                        <suiteXmlFile>testng.xml</suiteXmlFile>
                    </suiteXmlFiles>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

9.2 新建一個實現 IReporter 接口的類

在項目目錄src/main/java下的包com.mytest.httpclient下新建一個實現了 IReporter 接口的類,類名爲ExtentTestNGReporterListener,其代碼可點擊該鏈接查看。

9.3  創建 testng.xml 文件並添加相應的監聽器

右鍵點擊項目名,在彈出的菜單中選擇 TestNG → Convert to TestNG,在彈出的窗口中點擊 Finish 按鈕,這樣能夠自動生成項目的 testng.xml 文件。

在 testng.xml 中把剛纔創建的類 ExtentTestNGReporterListener 加入到監聽器中,這樣在測試運行時這個監聽器纔會起作用。最後完成的 testng.xml 文件內容如下:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "https://testng.org/testng-1.0.dtd">
<suite name="Suite">
    <listeners>
        <listener class- name="com.mytest.httpclient.ExtentTestNGReporterListener" />
    </listeners>
    <test thread-count="5" name="Test">
        <classes> 
            <class name="com.mytest.httpclient.test.GetTest"/>
            <class name="com.mytest.httpclient.test.RequestMethodTest"/>
        </classes>
    </test> 
</suite> 

9.4 使用 Maven 執行測試

最後,我們用 Maven 來執行下測試,這樣可以自動生成 extent reporters的測試報告。

右鍵點擊項目名,點擊 Run As →7 Maven test,這樣會以 Maven 的方式自動執行測試並生成相應的測試報告,執行完畢後,我們點擊 test-output 文件 夾中的 index.html,就會看到生成的 extent reporters 格式的測試報告。

未完待續......

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