TestNG+Ant 测试报告美化

来源:互联网 发布:最好的网络机顶盒 编辑:程序博客网 时间:2024/05/29 15:28

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

[java] view plaincopyprint?
  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. }  

5.src下建testng.xml
[html] view plaincopyprint?
  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>  

6.可以通过File-export-Ant的方式,将项目转化为Ant项目,会自动生成build.xml,然后修改代码为:
[html] view plaincopyprint?
  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>  

7. 右键 build.xml 文件,Run as -- Ant build

8. testoutput中 生成测试美化后的报告

注意:

1.

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

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

[html] view plaincopyprint?
  1. <path id="classes">  
  2.     <fileset dir="${libdir}" includes="*jar"/>  
  3.     <fileset dir="${libdir}" includes="*zip"/>  
  4.     <pathelement location="${basedir}/bin/"/>  
  5.   
  6. </path>  
 
[html] view plaincopyprint?
  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>  

3.testng-results.xml在路径(testng-xslt-1.1.2-master\test\single)下
[html] view plaincopyprint?
  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>  

0 0
原创粉丝点击