Testng在maven中配置Reportng及执行
Maven的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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.cubic.autohome</groupId>
<artifactId>autohome</artifactId>
<packaging>war</packaging>
<version>1.0-SNAPSHOT</version>
<name>autohome Maven Webapp</name>
<url>http://maven.apache.org</url>
<dependencies>
<!--
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
-->
<dependency>
<groupId>org.apache.maven</groupId>
<artifactId>maven-ant-tasks</artifactId>
<version>2.1.3</version>
</dependency>
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<version>6.8</version>
</dependency>
<dependency>
<groupId>velocity</groupId>
<artifactId>velocity-dep</artifactId>
<version>1.4</version>
</dependency>
<dependency>
<groupId>velocity</groupId>
<artifactId>velocity</artifactId>
<version>1.4</version>
</dependency>
<dependency>
<groupId>org.uncommons</groupId>
<artifactId>reportng</artifactId>
<version>1.1.5</version>
<!--<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>3.0</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<finalName>autohome</finalName>
<plugins>
<!-- 添加插件,添加ReportNg的监听器,修改最后的TestNg的报告 -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.5</version>
<configuration>
<suiteXmlFiles>
<file>testNG.xml</file>
</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>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<!--<xmlFileName>testNG.xml</xmlFileName>-->
<!--<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>-->
</properties>
</project>
在testng的配置文件中配置listeners:
<?xml version="1.0" encoding="utf-8" ?>
<suite name="Default suite" parallel="false">
<test name="testdemo">
<classes>
<class name="Car.CarTest"/>
</classes>
<listeners>
<listener class-name="org.uncommons.reportng.HTMLReporter" />
<listener class-name="org.uncommons.reportng.JUnitXMLReporter" />
</listeners>
</test>
</suite>
Testng中文乱码问题
使用maven进行构建导入官网的testng包后,然后执行mvn test,执行testng.xml配置文件时,生成的报告中会存在乱码问题。
原因是要修改AbstractReporter中的generateFile方法中
//Writer writer = new BufferedWriter(new FileWriter(file));
//encoding to utf-8
OutputStream ut=new FileOutputStream(file);
Writer writer = new BufferedWriter(new OutputStreamWriter(out,"utf-8"));
修改完成之后还要重新打包为jar包,如下,是我在pom.xml中配置的(注意:包名必须保持一致,不能修改)。
<groupId>org.uncommons</groupId>
<artifactId>reportng</artifactId>
<version>1.1.5</version>
将修改好的jar包导入到本地maven仓库才能像上边这样引用:
http://blog.csdn.net/liuchang__/article/details/72877895
如上链接说明了怎样添加。当然也可以上传到公司的maven仓库,然后在pom中引用也可以。
Reportng-1.1.5的jar包已经上传到我自己的csdn,后续如果要找jar包,直接去csdn查找下载。
HTML Report在jenkins上显示
首先根据这个链接下载HTML Publisher Plugin插件:http://www.cnblogs.com/EasonJim/p/6295011.html
Jenkins在配置完HTML Publisher插件后,在配置没有问题情况下打开html report时会显示空白,根据网上的说法,是因为安全问题导致的,这里记录下来,以后遇到类似问题可以查看:
参考链接:
解决Jenkins中无法展示HTML样式问题:https://zhuanlan.zhihu.com/p/28080975
https://testerhome.com/topics/9185
注意:修改完成后,一定要注意看Result下的结果为Result:,并且必须重启Tomcat服务。
Testng能做的事整理:
Testng忽略测试
如果代码还没有准备就绪,需要禁用某个测试用例:
需要在用例中添加@Test(enabled=false)。
Testng用例超时限制测试
如果需要让某个用例测试时间在某个范围内,需要添加超时时间:
添加@Test(timeOut=5000) 5000毫秒超时时间
Testng分组测试
@Test(group=”demo_test”)使用分组测试可以进行某个模块分类管理等。
具体使用可参考:http://www.yiibai.com/testng/testng-groups.html
Testng指定包名称进行测试,指定包含或排除某些方法,指定包含或排除某些分组等配置:http://www.yiibai.com/testng/suite-test.html
Testng依赖测试
在@Test注释中使用属性 dependsOnMethods或者,
在@Test注释中使用属性 dependsOnGroups。
如果依赖方法失败,则跳过所有后续测试方法。
http://www.yiibai.com/testng/dependency_test.html
Testng参数化方式测试
@Parameters和@DataProvider方式传递,详细实例:
http://www.yiibai.com/testng/parameterized-test.html