1. 项目设置
a.testng.xml的配置
这个是根据自己项目的来设置测试套件的名称、分组、类、方法,可设置成按顺序执行测试,文件名位置可自行修改。
例子:
<?xml version="1.0" encoding="UTF-8"?>
<suite name="my-webdriver">
<test name="Mytest" preserve-order="true"><!-- preserve-order=true用于设置test方法按顺序执行 -->
<classes>
<class name="my.test.TheExcelTest">
<methods>
<include name="ts" />
</methods>
</class>
</classes>
</test>
</suite>
b.ant的配置
在项目根目录下新建build.xml文件,在文件中设置任务依赖,文件名位置可自行修改。
例子:
<?xml version="1.0" encoding="UTF-8"?>
<project basedir="." default="testoutput" name="automation test">
<property name="base.dir" value="E:/workplace/WebDriverDemo"/>
<!--项目根目录-->
<property name="testng.output.dir" value="${base.dir}/test-output"/>
<property name="lib.dir" value="${base.dir}/lib"/>
<property name="testng.file" value="testng.xml"/> <!--调用的testng用例执行xml文件,这里是文件名-->
<taskdef resource="testngtasks" classpath="${lib.dir}/testng.jar"/>
<target name="clean">
<delete dir="${base.dir}/bin"/>
</target>
<target name="compile" depends="clean">
<mkdir dir="${base.dir}/bin"/>
<javac srcdir="${base.dir}/src" encoding="UTF-8" destdir="${base.dir}/bin" classpathref="classes" includeantruntime="off" debug="on" debuglevel="lines,vars,source"/>
</target>
<path id="classes">
<fileset dir="${lib.dir}" includes="*jar"/>
<fileset dir="${lib.dir}" includes="*zip"/>
<pathelement location="${base.dir}/bin"/>
</path>
<target name="runtest" depends="compile">
<testng outputdir="${testng.output.dir}" classpathref="classes" delegateCommandSystemProperties="true">
<jvmarg value="-Dfile.encoding=UTF-8"/>
<!--解决teng.xml中文输出在控制台中乱码问题-->
<xmlfileset dir="${base.dir}/src" includes="${testng.file}"/> <!--在指定路径下,找文件名由testng.file-->
</testng> <!--定义的testng.xml文件-->
</target>
<path id= "test.classpath" >
<fileset dir= "${lib.dir}" includes= "*.jar" />
</path>
<target name= "testoutput" depends="runtest" >
<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= "E:/workplace/WebDriverDemo/test-output/" />
<classpath refid= "test.classpath" />
</xslt>
</target>
</project>
其中testoutput是后期美化testNG的测试报告用的,如果不用则将第二行的default="testoutput"修改为runtest即可。
至此配置完成,eclipse中右击build.xml运行ant即可,也可单独运行ant,可在命令行下切换到ant目录用 ant –F f:uild.xml 来运行
1.修改相应目录
2.在JobAutoTest 创建lib文件夹(该文件用于存放项目中用到的所有jar文件,方便管理)
3.将selenium的安装程序复制到 lib中 以及 testng的jar包C:eclipsepluginsorg.testng.eclipse_6.8.6.20141201_2240lib
4.在项目根目录下新建build.xml文件,并修改build.xml中的内容
5.在src下建立testng.xml,并修改testng.xml中的内容
6.项目运行:在Build.xml上右键 run as ----- ant build,该项目即被运行。
以上为利用ant调用testng的xml文件运行系统。
下面为测试报告的美化方法
a. testng报告美化的方法
1.下载 TestNG-xslt 把 saxon-8.7.jar
2.复制到测试项目的 lib 下
3. 从你下载的包中拷贝文件 testng-results.xsl 到 test-output 目录下。 testng-results.xsl 文件的位置是testng-xslt-1.1.1/src/main/resources ,为什么要这个文件呢?因为我们的测试报告就是用这个 style 生成的。
4.在上面的build.xml中将第二行的写成default="testoutput"即可。
效果图:
相关标签: