JUnit on Ant

色々なページからの寄せ集め。
大昔に作ったので何処から持ってきたか。。

<!-- Junitを実行する -->
  <!-- testsをproperty化しているのは以下の手順で -->
  <!-- 1つのテストのみを実行できるようにするため -->
  <!-- ant -Dtests=Test名 -->
  <!-- AllTestsをexcludeをするのはtestsuiteを除外するため -->
  <!-- プロジェクトに合わせた形に変更すること -->
<target name="run.junit" depends="compile">
	<delete dir="${test.reports.dir}"/>
	<mkdir dir="${test.reports.dir}"/>
	<property name="tests" value="*Test*"/>
	<junit printsummary="true" haltonerror="false" haltonfailure="false" fork="true"
		errorproperty="testsuite.error" failureproperty="testsuite.failure">
		<classpath>
			<path refid="class.path"/>
		</classpath>
		<formatter type="plain" usefile="false"/>
		<formatter type="xml"/>
		<batchtest>
			<fileset dir="${build.dir}">
				<include name="${tests}.java"/>
				<exclude name="**/AllTests.java"/>
			</fileset>
		</batchtest>
	</junit>
</target>


<!-- Junitのレポートを出力 -->
  <!-- テスト結果の格納されているXMLをHTMLに変換して -->
  <!-- ${test.report.html.dir}に保存する -->
  <!-- APACHE XML Projectのxalan.jarが必要 -->
<target name="create.junitReport" depends="run.junit">
	<delete dir="${test.report.html.dir}"/>
	<mkdir dir="${test.report.html.dir}"/>

	<!-- XMLファイルを基にHTML形式のレポートを出力 -->
	<junitreport>
		<fileset dir="${test.reports.dir}">
			<include name="TEST-*.xml"/>
		</fileset>
		<report format="frames" todir="${test.report.html.dir}"/>
	</junitreport>

	<!-- 実行時間を知るためにHTMLの一部を現在の日付時刻に置換 -->
	<replace file="${test.report.html.dir}/overview-summary.html"
		token="Unit Test Results"
		value="Unit Test Results - ${NOW}"/>
</target>

<!-- テストが失敗したときの処理 -->
  <!-- ${testsuite.error} or ${testsuite.failure}がtureの時 ビルドをfail終了 -->
  <!-- 失敗したら即fail終了ではレポート出力の意味が無いので  -->
  <!-- run.junitのhaltonerror,haltonfailureをfalseにしている -->
<target name="test.result.check" depends="run.junit,create.junitReport">
	<condition property="must.fail">  
		<or>
			<istrue value="${testsuite.error}"/>
			<istrue value="${testsuite.failure}"/>
		</or>
	</condition>

	<!-- fail終了にしているのはMailLoggerにテスト失敗を通知するため -->
	<fail message="Tests didn't run 100%. Check the log and make necessary changes!" 
		if="must.fail"/>
</target>