EclEmma测试,ant生成reports


1. Eclipse插件安装:

2. Ant生成junit报告

  • 先看代码
<target name="junit" depends="clean,build-test">
		<mkdir dir="${report.dir}" />
		<junit printsummary="on" fork="false" showoutput="true">
			<classpath refid="master-classpath" />
			<formatter type="xml" />
			<batchtest todir="${report.dir}">
				<fileset dir="${build.dir}">
					<include name="tests/Test*" />
				</fileset>
			</batchtest>
		</junit>
		<junitreport todir="${report.dir}">
			<fileset dir="${report.dir}">
				<include name="TEST-*.xml" />
			</fileset>
			<report format="frames" todir="${report.dir}" />
		</junitreport>
	</target>
  • 这里省略了两个依赖:clean,build-test,其中clean清理生成的class文件,build-test是对要生成报告的类的编译
  • master-classpath运行需要的Jar包:这里是junit4
  • report.dir是报告生成目录;build.dir是编译class的路径
  • junit任务包含两步:junit运行测试(junit),生成报告(junitreport)

EclEmma生成测试覆盖度

  • 代码:
<target name="coverage">
        <jacoco:coverage destfile="${basedir}/jacoco.exec">
            <junit haltonfailure="false" fork="true" printsummary="true">
                <classpath refid="master-classpath"/>
                <formatter type="xml" />
                <batchtest todir="${report.dir}" fork="true">
                	<fileset dir="${build.dir}">
						<include name="tests/Test*" />
					</fileset>
                </batchtest>
            </junit>
        </jacoco:coverage>
    </target>
<target name="coverreport" depends="coverage">
        <jacoco:report>
            <executiondata>
                <file file="${basedir}/jacoco.exec" />
            </executiondata>
            <structure name="unit test coverage rate report.">
                <classfiles>
                	<fileset dir="${build.dir}">
						<include name="tests/Test*" />
					</fileset>
                </classfiles>
                <sourcefiles encoding="UTF-8">
                	<fileset dir="${src.dir}">
						<include name="tests/Test*" />
					</fileset>
                </sourcefiles>
            </structure>
            <html footer="footer" destdir="${report.dir.coverage}" />
            <csv destfile="${report.dir.coverage}/coverage-report.csv" />
            <xml destfile="${report.dir.coverage}/coverage-report.xml" />
        </jacoco:report>
    </target>
	 <taskdef uri="antlib:org.jacoco.ant" resource="org/jacoco/ant/antlib.xml">
	 	<classpath refid="jacoco-classpath"/>
	 </taskdef>
  • report.dir.coverage是覆盖率存放路径
01-03 12:16