.
Junit、Spock自动化单元测试
在java或groovy等语言开发的项目中,我们一般把需要自动执行的测试方法写在test文件夹中,与源代码分开。单元测试的方法可以使用junit或spock等测试框架,在项目构建、打包或发布的时候,这些测试方法需要自动执行,如果都顺利通过,才能执行 构建、打包或发布 等任务。
在maven或gradle项目中,实现这一目标比较简单,只需要执行test任务即可。但在 apache ant 构建项目时,自动化单元测试的执行则需要更多的配置。
如果使用junit的项目,在build.xml 文件中,配置junit任务执行测试,配置junitreport任务生成测试报告,示例如下:
<target name="test auot junit and report" depends="all compile">
<junit printsummary="on" fork="true" showoutput="true">
<classpath>
<fileset dir="${lib.dir}" includes="**/*.jar"/>
<pathelement path="${output folder}"/>
</classpath>
<formatter type="xml"/>
<batchtest todir="${report folder}">
<fileset dir="${output folder}">
<include name="**/Test*.*"/>
</fileset>
</batchtest>
</junit>
<junitreport todir="${report folder}">
<fileset dir="${report folder}">
<include name="TEST-*.xml"/>
</fileset>
<report format="frames" todir="${report folder}"/>
</junitreport>
</target>
如果是spock测试方法,自动化执行则需要自己定义可执行的任务,可使用 apache ant 的内置任务 exec 编写,在构建打包之前执行自定义的测试任务。
如果你不想再体验一次自学时找不到资料,没人解答问题,坚持几天便放弃的感受的话,可以常去一些技术论坛、群。多和别人交流,探讨一些技术上的问题。在这里向大家推荐(785128166)、群,有技术大牛解惑答疑,还有各类资源(面试题,视频教程,PDF文档)