TestNG本身的测试报告很不美观,我们可以下载testng-xslt,加入其中的jar包去进行美化。该效果比reportNG还要美观。
1.下载testng-xslt(如testng-xslt-1.1.2版本)
其中lib下的saxon-8.7.jar 是用来美化report的jar。
2.新建JAVA项目,在项目下新建一个lib文件夹,用来加入jar文件;新建testoutput文件夹,在其中加入testng-results.xsl (路径:testng-xslt-1.1.2-master\src\main\resources)用来承再生成美化的测试报告。
3.lib里面加入需要的jar文件, 并引用。
4.TestNGSimpleTest.java
- import org.testng.annotations.Test;
- import static org.testng.Assert.assertEquals;
- public class TestNGSimpleTest {
- @Test
- public void testAdd() {
- String str = "TestNG is working fine";
- assertEquals("TestNG is working fine", str);
- }
- }
import org.testng.annotations.Test;
import static org.testng.Assert.assertEquals;
public class TestNGSimpleTest {
@Test
public void testAdd() {
String str = "TestNG is working fine";
assertEquals("TestNG is working fine", str);
}
}
5.src下建testng.xml
- <?xml version="1.0" encoding="UTF-8"?>
- <!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >
- <suite name="Suite1">
- <test name="test1">
- <classes>
- <class name="TestNGSimpleTest"/>
- </classes>
- </test>
- </suite>
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >
<suite name="Suite1">
<test name="test1">
<classes>
<class name="TestNGSimpleTest"/>
</classes>
</test>
</suite>
6.可以通过File-export-Ant的方式,将项目转化为Ant项目,会自动生成build.xml,然后修改代码为:
- <project name="TestNGTest" default="testoutput" basedir=".">
- <!-- Define <testng> task -->
- <taskdef name="testng" classname="org.testng.TestNGAntTask">
- <classpath>
- <pathelement location="lib/testng-6.8.jar" />
- </classpath>
- </taskdef>
- <property name="testoutputdir" location="testoutput" />
- <property name="srcdir" location="src" />
- <property name="libdir" location="lib" />
- <property name="full-compile" value="true" />
- <property name="basedir" value="D:/workspace/TestNGSimple/" />
- <path id="classpath.test">
- <fileset dir="${libdir}">
- <include name="**/*.jar" />
- </fileset>
- <pathelement location="${testoutputdir}" />
- <pathelement location="${srcdir}" />
- </path>
- <target name="clean">
- <delete dir="${basedir}/bin" />
- </target>
- <target name="compile" depends="clean">
- <mkdir dir="${basedir}/bin" />
- <javac srcdir="${srcdir}" encoding="UTF-8" destdir="${basedir}/bin" verbose="${full-compile}" classpathref="classpath.test" includeantruntime="off" debug="on" debuglevel="lines,vars,source" />
- </target>
- <path id="classes">
- <fileset dir="${libdir}" includes="*jar"/>
- <fileset dir="${libdir}" includes="*zip"/>
- <pathelement location="${basedir}/bin/"/>
- </path>
- <target name="runtest" depends="compile">
- <testng outputdir="${testoutputdir}" classpathref="classes" delegateCommandSystemProperties="true">
- <xmlfileset dir="${srcdir}" includes="testng.xml" />
- </testng>
- </target>
- <target name="testoutput" depends="runtest">
- <xslt in="${testoutputdir}/testng-results.xml" style="${testoutputdir}/testng-results.xsl" out="${testoutputdir}/index.html ">
- <param name="testNgXslt.outputDir" expression="D:/workspace/TestNGSimple/testoutput/" />
- <classpath refid="classpath.test" />
- </xslt>
- </target>
- </project>
<project name="TestNGTest" default="testoutput" basedir=".">
<!-- Define <testng> task -->
<taskdef name="testng" classname="org.testng.TestNGAntTask">
<classpath>
<pathelement location="lib/testng-6.8.jar" />
</classpath>
</taskdef>
<property name="testoutputdir" location="testoutput" />
<property name="srcdir" location="src" />
<property name="libdir" location="lib" />
<property name="full-compile" value="true" />
<property name="basedir" value="D:/workspace/TestNGSimple/" />
<path id="classpath.test">
<fileset dir="${libdir}">
<include name="**/*.jar" />
</fileset>
<pathelement location="${testoutputdir}" />
<pathelement location="${srcdir}" />
</path>
<target name="clean">
<delete dir="${basedir}/bin" />
</target>
<target name="compile" depends="clean">
<mkdir dir="${basedir}/bin" />
<javac srcdir="${srcdir}" encoding="UTF-8" destdir="${basedir}/bin" verbose="${full-compile}" classpathref="classpath.test" includeantruntime="off" debug="on" debuglevel="lines,vars,source" />
</target>
<path id="classes">
<fileset dir="${libdir}" includes="*jar"/>
<fileset dir="${libdir}" includes="*zip"/>
<pathelement location="${basedir}/bin/"/>
</path>
<target name="runtest" depends="compile">
<testng outputdir="${testoutputdir}" classpathref="classes" delegateCommandSystemProperties="true">
<xmlfileset dir="${srcdir}" includes="testng.xml" />
</testng>
</target>
<target name="testoutput" depends="runtest">
<xslt in="${testoutputdir}/testng-results.xml" style="${testoutputdir}/testng-results.xsl" out="${testoutputdir}/index.html ">
<param name="testNgXslt.outputDir" expression="D:/workspace/TestNGSimple/testoutput/" />
<classpath refid="classpath.test" />
</xslt>
</target>
</project>
7. 右键 build.xml 文件,Run as -- Ant build
8. testoutput中 生成测试美化后的报告
注意:
1.
- <project name="TestNGTest" default="testoutput" basedir=".">
<project name="TestNGTest" default="testoutput" basedir=".">
default=“testoutput”,如果不需要美化的报告,则改为runtest
2.注意classes 的配置,然后引入到runtest的配置中,否则报找不到class的异常。
- <path id="classes">
- <fileset dir="${libdir}" includes="*jar"/>
- <fileset dir="${libdir}" includes="*zip"/>
- <pathelement location="${basedir}/bin/"/>
- </path>
<path id="classes">
<fileset dir="${libdir}" includes="*jar"/>
<fileset dir="${libdir}" includes="*zip"/>
<pathelement location="${basedir}/bin/"/>
</path>
- <target name="runtest" depends="compile">
- <testng outputdir="${testoutputdir}" classpathref="classes" delegateCommandSystemProperties="true">
- <xmlfileset dir="${srcdir}" includes="testng.xml" />
- </testng>
- </target>
<target name="runtest" depends="compile">
<testng outputdir="${testoutputdir}" classpathref="classes" delegateCommandSystemProperties="true">
<xmlfileset dir="${srcdir}" includes="testng.xml" />
</testng>
</target>
3.testng-results.xml在路径(testng-xslt-1.1.2-master\test\single)下
- <target name="testoutput" depends="runtest">
- <xslt in="${testoutputdir}/testng-results.xml" style="${testoutputdir}/testng-results.xsl" out="${testoutputdir}/index.html ">
- <param name="testNgXslt.outputDir" expression="D:/workspace/TestNGSimple/testoutput/" />
- <classpath refid="classpath.test" />
- </xslt>
- </target>
<target name="testoutput" depends="runtest">
<xslt in="${testoutputdir}/testng-results.xml" style="${testoutputdir}/testng-results.xsl" out="${testoutputdir}/index.html ">
<param name="testNgXslt.outputDir" expression="D:/workspace/TestNGSimple/testoutput/" />
<classpath refid="classpath.test" />
</xslt>
</target>