ant脚本执行junit测试用例
2014-01-07 14:30
411 查看
除了使用java来直接运行junit之外,我们还可以使用ant脚本,结合脚本执行junit用例,并生成测试报告,在进行每日构建等动作时非常有用。
一个完整的例子:
[html]
view plaincopyprint?
<?xml version="1.0"?> <project name="project" default="test"> <property name="build" value="bin"></property> <property name="src" value="src"></property> <property name="src-test" value="src-test"></property> <property name="lib" value="../lib"></property> <property name="testReport" value="testReport"></property> <path id="javac-lib"> <fileset dir="${lib}"> <include name="*.jar" /> </fileset> <!--缺少以下的设置可能出现ClassNotFoundException错误--> <pathelement location="${build}"/> </path> <target name="compile"> <javac destdir="${build}" debug="true" encoding="UTF-8"> <src path="src" /> <classpath refid="javac-lib" /> </javac> </target> <target name="test" depends="compile"> <delete dir="${testReport}" /> <mkdir dir="${testReport}" /> <!--执行JUnit测试用例--> <junit printsummary="yes"> <classpath refid="javac-lib"/> <formatter type="xml"/> <batchtest todir="${testReport}"> <fileset dir="${build}"> <include name="**/*Test.class"/> </fileset> </batchtest> </junit> <!--生成html的测试报告--> <junitreport todir="${testReport}"> <fileset dir="${testReport}"> <include name="TEST-*.xml"/> </fileset> <report format="frames" todir="${testReport}"/> </junitreport> <!--删除xml的测试结果--> <delete dir="${testReport}"> <include name="*.xml" /> </delete> </target> </project>
以上实例按几个步骤执行:
1、设置了代码路径、编译环境等
2、执行javac编译target
3、执行junit测试target
其中junit target又按几个步骤执行:
1、初始化报告输出目录
2、执行junit用例,'<junit printsummary="yes">'
3、<formatter type="xml"/>生成xml的测试结果
4、batchtest批量执行用例
5、junitreport根据每个xml用例执行结果生成html格式报告
6、删除每个xml用例结果,如果想看生成xml格式结果,可以把这一步屏蔽
PS:
执行过程中发生ClassNotFoundException错误的解决办法,注意在环境中是否加入了你的编译路径,<pathelement location="${build}"/>参考:http://yusudong.iteye.com/blog/1134915
JUnit Task详细的参数以及各参数的描述可以参考官网的说明:http://ant.apache.org/manual/Tasks/junit.html
一个完整的例子:
[html]
view plaincopyprint?
<?xml version="1.0"?> <project name="project" default="test"> <property name="build" value="bin"></property> <property name="src" value="src"></property> <property name="src-test" value="src-test"></property> <property name="lib" value="../lib"></property> <property name="testReport" value="testReport"></property> <path id="javac-lib"> <fileset dir="${lib}"> <include name="*.jar" /> </fileset> <!--缺少以下的设置可能出现ClassNotFoundException错误--> <pathelement location="${build}"/> </path> <target name="compile"> <javac destdir="${build}" debug="true" encoding="UTF-8"> <src path="src" /> <classpath refid="javac-lib" /> </javac> </target> <target name="test" depends="compile"> <delete dir="${testReport}" /> <mkdir dir="${testReport}" /> <!--执行JUnit测试用例--> <junit printsummary="yes"> <classpath refid="javac-lib"/> <formatter type="xml"/> <batchtest todir="${testReport}"> <fileset dir="${build}"> <include name="**/*Test.class"/> </fileset> </batchtest> </junit> <!--生成html的测试报告--> <junitreport todir="${testReport}"> <fileset dir="${testReport}"> <include name="TEST-*.xml"/> </fileset> <report format="frames" todir="${testReport}"/> </junitreport> <!--删除xml的测试结果--> <delete dir="${testReport}"> <include name="*.xml" /> </delete> </target> </project>
<?xml version="1.0"?> <project name="project" default="test"> <property name="build" value="bin"></property> <property name="src" value="src"></property> <property name="src-test" value="src-test"></property> <property name="lib" value="../lib"></property> <property name="testReport" value="testReport"></property> <path id="javac-lib"> <fileset dir="${lib}"> <include name="*.jar" /> </fileset> <!--缺少以下的设置可能出现ClassNotFoundException错误--> <pathelement location="${build}"/> </path> <target name="compile"> <javac destdir="${build}" debug="true" encoding="UTF-8"> <src path="src" /> <classpath refid="javac-lib" /> </javac> </target> <target name="test" depends="compile"> <delete dir="${testReport}" /> <mkdir dir="${testReport}" /> <!--执行JUnit测试用例--> <junit printsummary="yes"> <classpath refid="javac-lib"/> <formatter type="xml"/> <batchtest todir="${testReport}"> <fileset dir="${build}"> <include name="**/*Test.class"/> </fileset> </batchtest> </junit> <!--生成html的测试报告--> <junitreport todir="${testReport}"> <fileset dir="${testReport}"> <include name="TEST-*.xml"/> </fileset> <report format="frames" todir="${testReport}"/> </junitreport> <!--删除xml的测试结果--> <delete dir="${testReport}"> <include name="*.xml" /> </delete> </target> </project>
以上实例按几个步骤执行:
1、设置了代码路径、编译环境等
2、执行javac编译target
3、执行junit测试target
其中junit target又按几个步骤执行:
1、初始化报告输出目录
2、执行junit用例,'<junit printsummary="yes">'
3、<formatter type="xml"/>生成xml的测试结果
4、batchtest批量执行用例
5、junitreport根据每个xml用例执行结果生成html格式报告
6、删除每个xml用例结果,如果想看生成xml格式结果,可以把这一步屏蔽
PS:
执行过程中发生ClassNotFoundException错误的解决办法,注意在环境中是否加入了你的编译路径,<pathelement location="${build}"/>参考:http://yusudong.iteye.com/blog/1134915
JUnit Task详细的参数以及各参数的描述可以参考官网的说明:http://ant.apache.org/manual/Tasks/junit.html
相关文章推荐
- Ant+JUnit+Cobertura执行测试用例时,应注意的问题(以Apache POI为例)
- Ant/Junit: 利用Ant执行测试用例并生成测试报告
- 对基于JUnit和Ant的测试用例执行过程使用Kieker(AspectJ)进行监控的方法
- 基于Ant,执行并生成Jmeter脚本的测试报告
- Ant执行JUnit测试并生成报告
- 使用Ant运行JUnit测试用例时,报java.lang.ClassNotFoundException: org.hamcrest.SelfDescribing的一种解决方案
- 性能测试培训:批量执行Jmeter脚本之ant调用
- Junit--按照顺序执行测试用例
- Ant学习-002-ant 执行 TestNG 测试用例时 [testng] java.lang.NoClassDefFoundError: com/beust/jcommander/ParameterException 解决方案
- ANT调用JMeter执行脚本并生成html测试报告
- Junit测试用例按顺序执行
- parasoft Jtest 使用教程:生成、执行和扩展Junit测试用例
- 性能测试培训:批量执行Jmeter脚本之ant调用 1
- 性能测试培训:批量执行Jmeter脚本之ant调用 2
- Ant执行JUnit测试并生成报告
- python webdriver执行所有测试用例脚本
- 为什么执行测试用例时报需要 org.junit 4???
- 用Ant批量执行Junit 测试
- 一种奇怪的错误,执行一个junit测试用例,代码却执行了很多遍
- parasoft Jtest 使用教程:生成、执行和扩展Junit测试用例