基于Java+Maven+Testng+Selenium+Log4j+Allure+Jenkins搭建一个WebUI自动化框架(4)集成Allure报表
通过集成Allure报表,可以让自动化测试结果以美观的图形化界面展现出来。集成步骤:
1、在pom.xml文件中添加allure依赖
<!--allure报表依赖-->
<dependency><groupId>io.qameta.allure</groupId><artifactId>allure-testng</artifactId><version>2.12.1</version><scope>test</scope>
</dependency>
2、在pom.xml文件中的<project>标签下设置属性,避免乱码
<properties><aspectj.version>1.9.7</aspectj.version><project.build.sourceEncoding>UTF-8</project.build.sourceEncoding><project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding><maven.compiler.encoding>UTF-8</maven.compiler.encoding>
</properties>
3、在<project>标签下引入Maven Surefire插件:生成Allure报表
<build><plugins><plugin><!-- maven-surefire-plugin 配合testng执行测试用例的maven插件 --><groupId>org.apache.maven.plugins</groupId><artifactId>maven-surefire-plugin</artifactId><version>2.22.1</version><configuration><!-- 测试失败后,是否忽略并继续测试 --><testFailureIgnore>true</testFailureIgnore><suiteXmlFiles><!-- testng配置文件名称 --><suiteXmlFile>testng.xml</suiteXmlFile></suiteXmlFiles><!--设置参数命令行 --><argLine><!-- UTF-8编码 -->-Dfile.encoding=UTF-8<!-- 配置拦截器 -->-javaagent:"${settings.localRepository}/org/aspectj/aspectjweaver/${aspectj.version}/aspectjweaver-${aspectj.version}.jar"</argLine><systemProperties><property><!-- 配置 allure 结果存储路径 --><name>allure.results.directory</name><value>${project.build.directory}/allure-results</value></property></systemProperties></configuration><dependencies><!-- aspectjweaver maven坐标 --><dependency><groupId>org.aspectj</groupId><artifactId>aspectjweaver</artifactId><version>${aspectj.version}</version></dependency></dependencies></plugin><plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-compiler-plugin</artifactId><configuration><source>8</source><target>8</target></configuration></plugin></plugins>
</build>
此步骤如果遇到<build>标签下某个包依赖解决不了,可以将这个包放到<project>标签下的<dependencies>标签后,再引入到<build>标签中
例如之前使用的aspectj是1.8.10由于依赖过于低,在Build中就出现了依赖报错的情况,此时我们单独在下面进行添加依赖,后就可以了
<dependency><groupId>org.aspectj</groupId><artifactId>aspectjweaver</artifactId><version>1.9.7</version></dependency>
至此,Allure报表的集成操作已经完成了,接下来就可以使用Allure报表生成测试报告。
通过Allure报表生成报告的操作:
(1)在工程目录下新建个testng.xml文件,此处的文件需要与上述Maven Surefire插件配置的testng.xml文件名一致,填入如下信息:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="All Test Suite" parallel="tests" thread-count="2"><test name="测试"><classes><class name="com.howentech.testcases.TestBaidu1"/><class name="com.howentech.testcases.TestBaidu2"/><class name="com.howentech.testcases.TestBaidu3"/></classes></test>
</suite>
其中的class是测试用例的类名,文件放置的目录如下图:
(2)在命令行执行命令:
mvn clean test
注意:必须使用maven构建测试执行,不能直接在测试类中执行或者在testng.xml中右键执行,那样是生成不了allure报表的。
输入完命令后回车,就会开始构建执行测试用例:
(3)生成allure报表:
mvn io.qameta.allure:allure-maven:serve
输入完后,就会生成了Allure报表:
可以清楚查看我们每条用例的执行情况: