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 格式的测试报告。

未完待续......

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