前言
这段时间看Testerhome的牛人做的一系列的自动化测试,采用的报告很多都是ReportNg的方式,所以自己也抽空想去做一下api的测试,顺便将ReportNg熟悉一下。这次主要是参考了ReportNG 手把手教你弄测试报告这篇文章,但是这篇文章的配置方式并不是Maven的方式,所以就总结一下吧。
正文
我们这里的话主要就是配置pom.xml文件
<?xml version="1.0" encoding="UTF-8"?>
<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.seewo</groupId>
<artifactId>EasiCareApiTest</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.3.6</version>
</dependency>
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<version>6.9.6</version>
</dependency>
<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>
<dependency>
<groupId>com.google.inject</groupId>
<artifactId>guice</artifactId>
<version>3.0</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.18.1</version>
<configuration>
<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>
<suiteXmlFiles>
<suiteXmlFile>testng.xml</suiteXmlFile>
</suiteXmlFiles>
<workingDirectory>target/</workingDirectory>
</configuration>
</plugin>
</plugins>
</build>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
</project>
ReportNg因为是依赖于TestNg的,所以说需要导入TestNg才行。再来是要将TestNG的defaultListener设置为false,下面配置了两个listener,一个是HTMLReport,用来生成HTML格式的Report,别一个是JUnitXMLReporter,这个是用来生成xml格式的report,用于jekins服务器
另外我们还需要制定执行测试用例集的文件 这里的标签就是 suiteXmlFiles,以及生成的测试报告的路径,这里指定的是当前路径的target的路径下。
下来就比较简单了运行mvn test,之后在target\surefire-reports\html查看结果。
但是同样的问题 Reportng 1.1.4版本是不支持,所以我们也是需要对源代码进行修改
修改AbstractReporter中的generateFile这个方法中的代码如下:
现在是这样的:
protected void generateFile(File file,String templateName,VelocityContext context) throws Exception
{
Writer writer = new BufferedWriter(new FileWriter(file));
try
{
Velocity.mergeTemplate(classpathPrefix + templateName,
ENCODING,
context,
writer);
writer.flush();
}
finally
{
writer.close();
}
}
修改成下面这样,然后编译好新的jar文件,编译新的jar报的方式是通过点击右侧的ant Build,加载build.xml文件 运行release就会在目录下生成一个release的文件加了,里面就会有我们需要的jar
protected void generateFile(File file,String templateName,VelocityContext context) throws Exception
{
//Writer writer = new BufferedWriter(new FileWriter(file));
//encoding to utf-8
OutputStream out=new FileOutputStream(file);
Writer writer = new BufferedWriter(new OutputStreamWriter(out,"utf-8"));
try
{
Velocity.mergeTemplate(classpathPrefix + templateName,ENCODING,context,writer);
writer.flush();
}
finally
{
writer.close();
}
}
这样生成的报告就不会乱码了。最后的话就是替换我们的report的jar包,修改成相同的jar文件名,丢在你本地的仓库就就可以了。
现在就能够支持中文了。这个只是个测试,后续会持续更新API测试的学习进度。
参考文章
Better looking HTML test reports for TestNG with ReportNG – Maven guide