您的位置:首页 > 其它

testng-xslt美化testng测试报告

2018-01-30 16:05 337 查看
TestNg测试结束后,会自动生成html测试报告,但是显示不够美观,可以说比较丑的(个人观点^_^)。我们可以使用testng-xslt来对TestNg生成的Hhtml测试报告进行重写、美化。

1.从官网下载testng-xslt并解压,解压后的目录:

testng-xslt下载链接:http://testng-xslt.googlecode.com/files/testng-xslt-1.1.2.zip



2.新建一个Java Project:



项目新建后,在src下新建一个class来编写测试用例,示例代码:

package test;

import org.testng.annotations.AfterMethod;
import org.testng.annotations.Test;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Ignore;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;

public class TestCase {

@BeforeMethod
public void BeforeMethod() {

}

@BeforeClass
public void setUp() throws Exception{
System.out.println("测试开始前的初始化!");
}

@Test
public void test1() throws Exception{
System.out.println("this is test method1.");
}

@Test
public void test2() throws Exception{
System.out.println("this is test method2.");
}

@Test
public void add() throws Exception{
int a=5;
int b=25;
System.out.println("j" + "= " + a + "+" + b);
}

@Test
public void test3() throws Exception{
System.out.println("Keydom is the best company!!");
}

@Ignore
public void Ignore() throws Exception{
System.out.println("this method do not run!");
}

@AfterMethod
public void AfterMethod() {

}

@AfterClass
public void tearDown() throws Exception{
System.out.println("测试结束后还原测试环境!");
}
}


3.测试用例新建好后,先运行一次,生成testng自带的html测试报告。生成的html测试报告在项目根目录的test-output文件下,点击index.html就可以查看了



testng生成的html报告展现界面:



4.接下来开始使用testng-xslt来重写测试报告:

(1)把 testng-xslt目录的 lib 文件下的saxon-8.7.jar 和 SaxonLiason.jar 拷贝到项目的lib文件夹(若没有则新建)下

创建lib文件时,一般在test-output的根目录下



(2)把  testng-xslt目录的 /src/main/resources/目录下的 testng-results.xsl 放到项目的test-output文件夹根目录下(若没有则新建)



(4)在test-output根目录下添加一个xml文件:



{name} :是项目的名字
{in}和{style}:对应的是testng生成报告的xml和xsl
{out}:是要用testNG-xslt生成报告的文件名和路径
{expression}:是要用testNG-xslt生成报告的路径

Note:{out}设置测试报告输出时,最好生成一个新的html(即:index1.html/indenewhtml)文件来展示测试报告,如代码中所示,否者build文件在编译的时候呈现的结果就是:ant编译成功,但是没有运行结果(如图所示)。



<?xml version="1.0" encoding="UTF-8"?>

<project name="SandyTestNg" basedir=".">
<property name="lib.dir" value="lib"/>

<path id="test.classpath">
<!-- adding the saxon jar to your classpath -->
<fileset dir="${lib.dir}" includes="*.jar"/>
</path>

<target name="transform">
<property name="dir" value=""/>

<!-- <mkdir dir="F:/eclipse-workspace-2/SandyTestNg/test-output/"/> -->

<xslt in="F:/eclipse-workspace-2/SandyTestNg/test-output/testng-results.xml"
style="F:/eclipse-workspace-2/SandyTestNg/test-output/testng-results.xsl"
out="F:/eclipse-workspace-2/SandyTestNg/test-output/index1.html"
classpathref="test.classpath" processor="SaxonLiaison">
<param name="testNgXslt.outputDir" expression="F:/eclipse-workspace-2/SandyTestNg/test-output" />
</xslt>
</target>
</project>

(5)运行build文件:右键→Run As →Ant build→Run
build文件运行成功后的提示:



走到这一步,那么恭喜,testng重写完成,点击test-output文件下的index.html就可以看到美美的测试报告了

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  TestNg