【JAVA】testNG报表美化

1. testng自带报表

我们使用testNG进行自动化测试时,在项目中test=output目录下有testNG自动生成的报表,
在这里插入图片描述
打开之后的网页如下图所示:
在这里插入图片描述
肉眼可见的丑啊,所以我们集成reportng,用来适配testng,来让他好看一些。

2.使用reportng

2.1 添加依赖

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

2.1 添加监听器

先配置好testng.xml,将测试用例执行类放入:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="Suite" parallel="false">
  <test name="api auto test">
    <classes>
      <class name="api.auto.testCase.All_Test_Case"/>
    </classes>
  </test> <!-- Test -->
</suite> <!-- Suite -->

然后在testng.xml中添加监听器,用来渲染报表;

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

在这里插入图片描述
然后右键testng.xml–>Run As–>TestNG Suite就可以了

2.3 Injector Not Found 解决方案

如果在上一步执行时最后报Injector Not Found 异常,如下图所示:
在这里插入图片描述
解决方案:

  1. 添加依赖库:guice
<dependency>
    <groupId>com.google.inject</groupId>
    <artifactId>guice</artifactId>
    <version>3.0</version>
</dependency>
  1. 更换testng的版本

2.4 查看报告

按照图片路径点击直接查看生成的报告:
在这里插入图片描述
在这里插入图片描述
点击test的name就可以直接进入并且查看测试用例运行的结果了。

3. surefire插件

使用suirefire插件同样也能够生成想上图那样的报告,而且为了后期集成jenkins等自动化平台,保证自己的脚本可以通过命令脚本来调动执行,最好使用surefire插件。
实现方法:集成maven的surefire插件, Surefire插件用于Maven项目的test阶段,以执行单元测试。

				<plugin>
					<groupId>org.apache.maven.plugins</groupId>
					<artifactId>maven-surefire-plugin</artifactId>
					<version>2.7.1</version>
					<configuration>
						<systemPropertyVariables>
							<org.uncommons.reportng.escape-output>false</org.uncommons.reportng.escape-output>
						</systemPropertyVariables>
						<testFailureIgnore>true</testFailureIgnore>
						<argLine>
							-Dfile.encoding=UTF-8
						</argLine>
						<suiteXmlFiles>
							<suiteXmlFile>testng.xml</suiteXmlFile>
						</suiteXmlFiles>
					</configuration>
				</plugin>

集成后我们就可以通过maven命令 maven test 来调动脚本执行了。
在这里插入图片描述
点击之后第一次会下载很多东西,耐心等待便可,最后运行完成之后控制台会输出如下信息:
在这里插入图片描述
其中标红的路径就是自动生成的此次测试报告的存储位置。

猜你喜欢

转载自blog.csdn.net/qq_34659777/article/details/88802910