转载大神文章在这:https://blog.youkuaiyun.com/galen2016/article/details/77165430
1.用maven管理依赖(maven-reportng插件依赖添加)pom.xml
<dependency>
<groupId>org.uncommons</groupId>
<artifactId>reportng</artifactId>
<version>1.1.4</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.google.inject</groupId>
<artifactId>guice</artifactId>
<version>3.0</version>
<scope>test</scope>
</dependency>
</dependencies>
<plugin>
<groupId>org.apache.maven.plugins</groupId><!--使用该插件设置TESTNG的配置文件位置-->
<artifactId>maven-surefire-plugin</artifactId>
<executions>
<execution>
<goals>
<goal>test</goal>
</goals>
</execution>
</executions>
<version>2.19</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>
<suitXmlFile>testng.xml</suitXmlFile><!--配置testng.xml文件的位置,我这里是在主目录下-->
</suiteXmlFiles>
<reportsDirectory>./target/${timestamp}</reportsDirectory>
<systemPropertyVariables>
<org.uncommons.reportng.escape-output>false</org.uncommons.reportng.escape-output>
</systemPropertyVariables>
</configuration>
</plugin>
useDefaultListeners = “false” 用来禁止TestNg产生报告,但是我们还需要他的错误报告testng-fails.xml文件,为了方便我们只关注未通过的测试,所以还要将TestNg的org.testng.reporters.FailedReporter监听器加上。
2.testng.xml添加reportng的监听
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="Default Suite">
<test name="Java_Learn">
<classes>
//按照类运行
<class name="testng.class001"/>(testng是要运行的包名)
</classes>
<listeners>
<listener class-name="org.uncommons.reportng.HTMLReporter" />
<listener class-name="org.uncommons.reportng.JUnitXMLReporter" />
</listeners>
</test> <!-- Java_Learn -->
</suite> <!-- Default Suite -->
3.默认运行的html结果在test-output/html文件夹下(上面的配置运行结果是这样的)
4.下面配置生成的报告在target/surefire-reports
https://www.cnblogs.com/dannyyao/p/6307719.html
testng.xml配置
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="Default Suite">
<test name="Java_Learn">
<classes>
<class name="testng.class001"/>
</classes>
<listeners>
<listener class-name="org.uncommons.reportng.HTMLReporter" />
<listener class-name="org.uncommons.reportng.JUnitXMLReporter" />
</listeners>
<usedefaultlisteners name="false" />
</test> <!-- Java_Learn -->
</suite> <!-- Default Suite -->
5.jenkins构建后测试报告显示空,是安全策略造成的,另外一种解决方式:
https://www.jianshu.com/p/16f5b01cc9c0
(在系统管理->脚本命令行,在里面输入System.setProperty(“hudson.model.DirectoryBrowserSupport.CSP”, “script-src ‘unsafe-inline’”), 点击运行)
看jenkins日志默认逻辑是:读取pom文件—加载插件或者依赖的包—maven-surefire-plugin这个插件非常重要(中间遇到在pom里面没有配置这个导致用jenkins运行根本不会运行测试,也就是说靠这个插件读取testng.xml文件并按照配置文件运行测试)