TestNG 强大的测试框架(5)-ant+xlst+testng

    前面我们讲了testng+xlst,本章我们来了解一下ant来运行testng并优化测试报告!

    首先我们的把testng框架搭好,并保证它能够正常的运行!(这里前面都有说过,就不再重复了)

    然后,我们创建一个build.xml文件,下面我们一起来解析一下build.xml里都有些什么内容->

    1.标签:property,声明变量,下面的代码有两个变量,${testng.dir}:指向项目的根目录,${testng.output.dir}:指向的是test-output文件,主要用于报告优化

<property name="testng.dir" value="D:\java\workspaces\GroupApi"/>  
<property name="testng.output.dir" value="D:\java\workspaces\GroupApi\test-output"/> 

  2.标签:path,路径;id,标签path的标识。标签:fileset,这里主要是找到项目中的jar包;dir,jar包在项目中的路径。标签:include,加载jar包。

<path id="run.classpath">
        <fileset dir="jar">
            <include name="testng\*.jar"></include>
		    <include name="httpcomponents-client-4.5.3\lib\*.jar"></include>
		    <include name="json\*.jar"></include>
		    <include name="poi-3.15\*.jar"></include>
		    <include name="poi-3.15\lib\*.jar"></include>
		    <include name="poi-3.15\ooxml-lib\*.jar"></include>
		    <include name="spring-framework-4.3.7.RELEASE\libs\*.jar"></include>
		    <include name="*.jar"></include>
        </fileset>
    </path>
    <path id="test.lib.classpath">
        <fileset dir="lib">
    		<include name="*.jar"></include>
    	</fileset>
    </path>

  3.标签:taskdef,具体是什么意思也不是太明白,但是我觉得应该是找到testng.jar里的TestNGAntTask类,用以ant运行testng!classpathref,导入项目需要的jar包!

<taskdef name="testng" classname="org.testng.TestNGAntTask" classpathref="run.classpath" />  

    4.标签:target,有点像java中类的感觉,可以有多个target标签,但是必须有一个标签的name必须与project的default的值一致。而target也可以依赖其他target。

               name:属性名

              depends:表示依赖的目标,有填这个值,会先执行依赖这个target。
             在实践中遇到的几个坑:
                             1.必须删除classpath目录,并重建classpath目录,这里指的是bin目录,项目运行时会把src目录下的文件copy到bin目录下,如果不删除的话,并重建bin目录的话,运行testng.xml时,会报异常:找不到指定的类。
                            2.运行java代码时的编码encoding="GBK",否则也有可能报异常。
<target name="clean">
        <delete dir="bin"/>
</target>
<target name="compile" depends="clean">
        <echo message="mkdir"/>
        <mkdir dir="bin"/>
        <javac srcdir="src"  includeantruntime="false"
            debug="on" debuglevel="lines,vars,source"  destdir="bin" encoding="GBK">
            <classpath refid="run.classpath"/>
        </javac>
    </target>
    <path id="runpath"> 
         <path refid="run.classpath"/> 
         <pathelement location="bin"/> 
    </path> 
    <target name="run" depends="compile">
        <testng  classpathref="runpath"  outputDir="test-output" 
            haltonfailure="true" failureproperty="test.failed">  
            <xmlfileset dir="${basedir}" includes="testng.xml"/>
        </testng>
     </target>

  4.表签:xslt,优化报告

                in,testng的运行结果

                style,把testng的运行结果进行ui美化

                out,输出优化报告

<xslt in="${testng.output.dir}/testng-results.xml" 
               style="${testng.output.dir}/testng-results.xsl" 
               out="${testng.output.dir}\index1.html">  
               <param name="testNgXslt.outputDir" expression="${testng.output.dir}"/>  
               <param name="testNgXslt.showRuntimeTotals" expression="true"/>  
               <classpath refid="test.lib.classpath"/>  
          </xslt>

  5.下面来看一看完整的build.xml文件:

<?xml version="1.0"?>
<project name="Demo" default="run" basedir=".">
    <property name="testng.dir" value="D:\java\workspaces\GroupApi"/>  
    <property name="testng.output.dir" value="D:\java\workspaces\GroupApi\test-output"/> 
    <path id="run.classpath">
        <fileset dir="jar">
            <include name="testng\*.jar"></include>
		    <include name="httpcomponents-client-4.5.3\lib\*.jar"></include>
		    <include name="json\*.jar"></include>
		    <include name="poi-3.15\*.jar"></include>
		    <include name="poi-3.15\lib\*.jar"></include>
		    <include name="poi-3.15\ooxml-lib\*.jar"></include>
		    <include name="spring-framework-4.3.7.RELEASE\libs\*.jar"></include>
		    <include name="*.jar"></include>
        </fileset>
    </path>
    <path id="test.lib.classpath">
        <fileset dir="lib">
    		<include name="*.jar"></include>
    	</fileset>
    </path>
    <taskdef name="testng" classname="org.testng.TestNGAntTask" classpathref="run.classpath" />   
    <target name="clean">
        <delete dir="bin"/>
    </target>
    <target name="compile" depends="clean">
        <echo message="mkdir"/>
        <mkdir dir="bin"/>
        <javac srcdir="src"  includeantruntime="false"
            debug="on" debuglevel="lines,vars,source"  destdir="bin" encoding="GBK">
            <classpath refid="run.classpath"/>
        </javac>
    </target>
    <path id="runpath"> 
         <path refid="run.classpath"/> 
         <pathelement location="bin"/> 
    </path> 
    <target name="run" depends="compile">
        <testng  classpathref="runpath"  outputDir="test-output" 
            haltonfailure="true" failureproperty="test.failed">  
            <xmlfileset dir="${basedir}" includes="testng.xml"/>
        </testng>
        <xslt in="${testng.output.dir}/testng-results.xml" 
               style="${testng.output.dir}/testng-results.xsl" 
               out="${testng.output.dir}\index1.html">  
               <param name="testNgXslt.outputDir" expression="${testng.output.dir}"/>  
               <param name="testNgXslt.showRuntimeTotals" expression="true"/>  
               <classpath refid="test.lib.classpath"/>  
        </xslt>
   </target> 
</project>

  6.以ant build方式运行build.xml文件

posted on 2017-05-12 15:27  菜鸟进阶历程  阅读(426)  评论(0)    收藏  举报

导航