先看build.xml
<?xml version="1.0" encoding="UTF-8"?> <project basedir="." default="testoutput" name="automation test"> <!--默认使用testoutput任务--> <property name="base.dir" value="E:/eclipse/workplace/testng"/><!--项目根目录--> <property name="testng.output.dir" value="${base.dir}/result"/><!--配置结果输出地址--> <property name="lib.dir" value="${base.dir}/lib"/><!--设置其他jar包目录--> <property name="testng.file" value="${base.dir}/testng.xml"/> <!--调用的testng用例执行xml文件,这里是文件名--> <property name="testdir" location="test" /> <!--设置编译的2进制文件目录--> <taskdef resource="testngtasks" classpath="${lib.dir}/testng.jar"/><!--导入testng的jar包--> <target name="clean"> <!--清除之前的2进制文件--> <delete dir="${testdir}"/> </target> <target name="compile" depends="clean"> <mkdir dir="${testdir}"/><!--新建2进制文件存放目录--> <mkdir dir="result"/> <!--新建结果导出目录--> <javac srcdir="${base.dir}/src" encoding="UTF-8" destdir="${testdir}" classpathref="classes" includeantruntime="off" debug="on" debuglevel="lines,vars,source"/> </target> <!--编译java程序--> <path id="classes"> <!--设置jar包相关--> <fileset dir="${lib.dir}" includes="*.jar"/> <pathelement location="${testdir}"/> <pathelement location="${base.dir}/src" /> </path> <target name="runtest" depends="compile"> <!--运行testng文件--> <!-- 在target里面新建一个testng标签,里面需要设置的属性有:outputdir – 测试结果输出目录;classpathref – 那些自动化测试代码的目标路径,通常就是编译完成以后的那个目标路径,例如xxx/bin;delegateCommandSystemProperties – 接受传递命令行参数作为系统变量,这个设置为true可以在调用Ant的时候通过 -Dfoo=value 把参数传递给TestNG;里面还有一个xmlfileset节点,这个节点就是指定testng.xml文件的目录以及具体文件。 --> <testng outputdir="${base.dir}/test-output" classpathref="classes" delegateCommandSystemProperties="true"> <xmlfileset dir="${base.dir}" includes="testng.xml"/> <!--在指定路径下,找文件名由testng.file--> </testng> <!--定义的testng.xml文件--> </target> <tstamp> <format property="CURTIME" pattern="yyyyMMdd_HHmmss" locale="us"/> <!--设置当前时间--> </tstamp> <path id= "test.classpath" > <fileset dir= "${lib.dir}" includes= "*.jar" /> </path> <target name= "testoutput" depends="runtest" > <xslt in= "test-output/testng-results.xml" style= "test-output/testng-results.xsl" out= "result/${CURTIME}/index.html " > <param name= "testNgXslt.outputDir" expression="${base.dir}/result/${CURTIME}/" /> <param name="testNgXslt.showRuntimeTotals" expression="true"/> <param name="testNgXslt.sortTestCaseLinks" expression="true" /> <param name="testNgXslt.testDetailsFilter" expression="FAIL,SKIP,PASS,CONF,BY_CLASS" /> <classpath refid= "test.classpath" /> </xslt> </target> </project>
在eclipse中,他的结构是:
project:
|
src(存放源代码)
|
lib(存放jar包)
|
test-output(存放testng的结果的目录)
|
testng.xml(testng的设置)
|
build.xml(ant的设置)
准备:
testng-xslt-1.1.2-master文件下载地址
testng.jar文件 下载地址
操作:
1.把testng-xslt-1.1.2-master中\lib\saxon-8.7.jar放入eclipse 的lib目录下
2.把testng-xslt-1.1.2-master中\src\main\resources\testng-results.xsl放入eclipse 的test-output的文件夹中
3.把上面的代码放入build.xml
4.点击build.xml右键run: Ant build