TestNG+Ant 测试报告美化

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

  1. import org.testng.annotations.Test;  
  2. import static org.testng.Assert.assertEquals;  
  3.   
  4. public class TestNGSimpleTest {  
  5.     @Test  
  6.     public void testAdd() {  
  7.         String str = "TestNG is working fine";  
  8.         assertEquals("TestNG is working fine", str);  
  9.     }  
  10. }  
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
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >  
  3. <suite name="Suite1">  
  4.   <test name="test1">  
  5.     <classes>  
  6.        <class name="TestNGSimpleTest"/>  
  7.     </classes>  
  8.   </test>  
  9. </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,然后修改代码为:
  1. <project name="TestNGTest" default="testoutput" basedir=".">  
  2.     <!-- Define <testng> task -->  
  3.     <taskdef name="testng" classname="org.testng.TestNGAntTask">  
  4.         <classpath>  
  5.             <pathelement location="lib/testng-6.8.jar" />  
  6.         </classpath>  
  7.     </taskdef>  
  8.     <property name="testoutputdir" location="testoutput" />  
  9.     <property name="srcdir" location="src" />  
  10.     <property name="libdir" location="lib" />  
  11.     <property name="full-compile" value="true" />  
  12.     <property name="basedir" value="D:/workspace/TestNGSimple/" />  
  13.   
  14.     <path id="classpath.test">  
  15.         <fileset dir="${libdir}">  
  16.             <include name="**/*.jar" />  
  17.         </fileset>  
  18.         <pathelement location="${testoutputdir}" />  
  19.         <pathelement location="${srcdir}" />  
  20.   
  21.     </path>  
  22.     <target name="clean">  
  23.         <delete dir="${basedir}/bin" />  
  24.   
  25.     </target>  
  26.   
  27.     <target name="compile" depends="clean">  
  28.         <mkdir dir="${basedir}/bin" />  
  29.         <javac srcdir="${srcdir}" encoding="UTF-8" destdir="${basedir}/bin" verbose="${full-compile}" classpathref="classpath.test" includeantruntime="off" debug="on" debuglevel="lines,vars,source" />  
  30.   
  31.     </target>  
  32.   
  33. <path id="classes">  
  34.     <fileset dir="${libdir}" includes="*jar"/>  
  35.     <fileset dir="${libdir}" includes="*zip"/>  
  36.     <pathelement location="${basedir}/bin/"/>  
  37.   
  38. </path>  
  39.   
  40.     <target name="runtest" depends="compile">  
  41.         <testng outputdir="${testoutputdir}" classpathref="classes" delegateCommandSystemProperties="true">  
  42.             <xmlfileset dir="${srcdir}" includes="testng.xml" />  
  43.         </testng>  
  44.     </target>  
  45.   
  46.     <target name="testoutput" depends="runtest">  
  47.         <xslt in="${testoutputdir}/testng-results.xml" style="${testoutputdir}/testng-results.xsl" out="${testoutputdir}/index.html ">  
  48.   
  49.             <param name="testNgXslt.outputDir" expression="D:/workspace/TestNGSimple/testoutput/" />  
  50.   
  51.             <classpath refid="classpath.test" />  
  52.   
  53.         </xslt>  
  54.   
  55.     </target>  
  56. </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.

  1. <project name="TestNGTest" default="testoutput" basedir=".">  
<project name="TestNGTest" default="testoutput" basedir=".">
default=“testoutput”,如果不需要美化的报告,则改为runtest

2.注意classes 的配置,然后引入到runtest的配置中,否则报找不到class的异常。

  1. <path id="classes">  
  2.     <fileset dir="${libdir}" includes="*jar"/>  
  3.     <fileset dir="${libdir}" includes="*zip"/>  
  4.     <pathelement location="${basedir}/bin/"/>  
  5.   
  6. </path>  
<path id="classes">
	<fileset dir="${libdir}" includes="*jar"/>
	<fileset dir="${libdir}" includes="*zip"/>
	<pathelement location="${basedir}/bin/"/>

</path>
 
  1. <target name="runtest" depends="compile">  
  2.       <testng outputdir="${testoutputdir}" classpathref="classes" delegateCommandSystemProperties="true">  
  3.           <xmlfileset dir="${srcdir}" includes="testng.xml" />  
  4.       </testng>  
  5.   </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)下
  1. <target name="testoutput" depends="runtest">  
  2.         <xslt in="${testoutputdir}/testng-results.xml" style="${testoutputdir}/testng-results.xsl" out="${testoutputdir}/index.html ">  
  3.   
  4.             <param name="testNgXslt.outputDir" expression="D:/workspace/TestNGSimple/testoutput/" />  
  5.   
  6.             <classpath refid="classpath.test" />  
  7.   
  8.         </xslt>  
  9.   
  10.     </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>

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值