testng+maven+java+idea 接口测试入门二:使用reportng优化报告格式

方法1:

1、将reportng信息配置到pom.xml中

<dependency>
    <groupId>org.uncommons</groupId>
    <artifactId>reportng</artifactId>
    <version>1.1.4</version>
    <scope>test</scope>
</dependency>

2、在testng.xml中添加监听信息,可以直接放在suite下:

<suite>
<listeners>
    <listener class-name="org.uncommons.reportng.HTMLReporter" />
    <listener class-name="org.uncommons.reportng.JUnitXMLReporter" />
</listeners>

...

...

</suite>

3、运行testng,运行之后会自动生产test-output目录,包含两个子文件夹,html和xml。html中包含一个index.html,拖入浏览器即可看到reportng的报告,如下:

方法2:

1、在pom.xml中增加配置maven sufire plugsin插件信息如下:

 <dependency>
        <groupId>org.uncommons</groupId>
        <artifactId>reportng</artifactId>
        <version>1.1.4</version>
        <scope>test</scope>
        <exclusions>
            <exclusion>
                <groupId>org.testng</groupId>
                <artifactId>testng</artifactId>
            </exclusion>
        </exclusions>
    </dependency>



    <!-- 依赖Guice -->

    <dependency>

        <groupId>com.google.inject</groupId>

        <artifactId>guice</artifactId>

        <version>4.0</version>

        <scope>test</scope>

    </dependency>

    <dependency>

        <groupId>velocity</groupId>

        <artifactId>velocity-dep</artifactId>

        <version>1.4</version>

    </dependency>

    <dependency>

        <groupId>log4j</groupId>

        <artifactId>log4j</artifactId>

        <version>1.2.17</version>

    </dependency>

</dependencies>

    <build>

        <plugins>


            <!-- 添加插件,添加ReportNg的监听器,修改最后的TestNg的报告 -->

            <plugin>

                <groupId>org.apache.maven.plugins</groupId>

                <artifactId>maven-surefire-plugin</artifactId>

                <version>2.5</version>

                <configuration>
                    <suiteXmlFiles>

                        <suiteXmlFile>testng.xml</suiteXmlFile>

                    </suiteXmlFiles>
                    <properties>

                        <property>

                            <name>usedefaultlisteners</name>

                            <value>false</value>

                        </property>

                        <property>

                            <name>listener</name>

                            <value>org.uncommons.reportng.HTMLReporter,org.uncommons.reportng.JUnitXMLReporter</value>

                        </property>

                    </properties>

                    <workingDirectory>target/</workingDirectory>

                    <forkMode>always</forkMode>

                </configuration>

            </plugin>

        </plugins>

    </build>
次配置将使得运行mvn test后的测试报告以reportng的形式生成到target/sufire-reports目录下面,同样包含htm和xml两个子文件夹,同样可以将html拖入浏览器进行浏览。


个人觉得,上述两个方法可以将reportng集成到测试项目中来,如果想用mvn来完成项目测试,可以采用第二种方式,这样不需要reportng重新生成test-output目录。用Jenkins集成项目时更适合一些。





  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
在进行接口自动化测试时,我们通常需要对接口进行请求并获取返回结果。而获取返回结果的方式一般有两种,一种是通过 HTTP 响应获取,另一种是通过解析 JSON 格式的响应体获取。 对于第一种方式,我们可以利用 Java 提供的 HttpURLConnection 类或者 Apache 的 HttpClient 库进行操作。而对于第种方式,我们可以使用 JSON 解析库来解析响应体。 下面介绍通过 HttpURLConnection 获取响应体的方法: ```java import java.io.BufferedReader; import java.io.InputStreamReader; import java.net.HttpURLConnection; import java.net.URL; public class HttpUtil { public static String doGet(String urlStr) { try { URL url = new URL(urlStr); HttpURLConnection conn = (HttpURLConnection) url.openConnection(); conn.setRequestMethod("GET"); BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream())); String line; StringBuilder result = new StringBuilder(); while ((line = in.readLine()) != null) { result.append(line); } in.close(); return result.toString(); } catch (Exception e) { e.printStackTrace(); } return null; } } ``` 上面的代码中,我们使用Java 提供的 HttpURLConnection 类来发送 GET 请求,并获取响应体。其中,URL 用于指定请求的地址,HttpURLConnection 用于建立与指定 URL 的连接,并发送请求。通过 BufferedReader 来读取响应体,并将其转换为字符串返回。 使用该方法可以轻松地获取到接口返回的响应体。接下来,我们可以通过 JSON 解析库来解析响应体,从而获取接口返回的具体数据。
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值