AntでJUnit 4.10のテストを実行する

antのbuild.xmlで、コンパイルと共にJUnitのテストを実行する方法のメモです。


ソースとテストケース



テスト対象のソースのサンプルがこちら。
このクラスの「add」メソッドをテストするケースを想定します。

  1. package sample;
  2. public class Main {
  3.     
  4.     public static void main(String[] args) {
  5.         System.out.println(add(10, 5));
  6.     }
  7.     
  8.     //引数2つを加えるだけ
  9.     public static int add(int a, int b) {
  10.         return a + b;
  11.     }
  12.     
  13. }




テストケースはこんな感じ。
10+5が15になるかテストしています。

  1. package sample;
  2. import org.junit.Test;
  3. import static org.junit.Assert.*;
  4. import static org.hamcrest.core.Is.is;
  5. public class MainTest {
  6.     
  7.     @Test
  8.     public void testAdd() {
  9.         assertThat(Main.add(10, 5), is(15));
  10.     }
  11. }





ソースはそれぞれ、

src/main/java/sample/Main.java
src/test/java/sample/MainTest.java


に配置し、JUnitのjarファイルは

lib/junit-4.10.jar


に配置しています。

Eclipseからだと、このように見えています。

100_01.png





build.xml



コンパイルとテストを実行し、最終的に実行可能なjarファイルを出力することを想定しています。
jarファイルにMainTest.classを含めたくないので、こんな方針で行きます。


・Main.javaをコンパイルし、buildフォルダに出力する。
・MainTest.javaをコンパイルし、testbuildフォルダに出力する。
(この時、Main.classとjunit.jarにclasspathを通す必要あり)
・JUnitによるテストを実行する。
(この時、Main.classとMainTest.class、junit.jarにclasspathを通す必要あり)
・buildフォルダのclassをjarで固める。




あまりbuild.xmlを書き慣れていないので、ちゃんと流儀に添えているか
微妙ですがこんな感じになりました。


  1. <?xml version="1.0" encoding="UTF-8" ?>
  2. <project name="sample" default="dist" basedir=".">
  3.     <description>
  4.         Ant + JUnitの連携サンプル
  5.     </description>
  6.     <!-- Mainのビルドとjarファイルの作成に必要な情報 -->
  7.     <property name="src" location="src/main/java"/>
  8.     <property name="build" location="build"/>
  9.     <property name="dist" location="dist"/>
  10.     <!-- MainTestのビルドに必要な情報 -->
  11.     <property name="test.src" location="src/test/java"/>
  12.     <property name="test.build" location="testbuild"/>
  13.     <property name="test.class.name" value="sample.MainTest" />
  14.     <path id="test.classpath">
  15.         <pathelement location="${build}" />
  16.         <pathelement location="${test.build}" />
  17.         <pathelement location="lib/junit-4.10.jar" />
  18.     </path>
  19.     <target name="init">
  20.         <!-- Create the time stamp -->
  21.         <tstamp/>
  22.         <mkdir dir="${build}"/>
  23.         <mkdir dir="${test.build}"/>
  24.     </target>
  25.     <target name="compile" depends="init" description="compile the source " >
  26.         <!-- Mainをビルドし、buildフォルダに出力 -->
  27.         <javac srcdir="${src}" destdir="${build}" includeAntRuntime="no"/>
  28.         <!-- MainTestをビルドし、testbuildフォルダに出力 -->
  29.         <javac srcdir="${test.src}" destdir="${test.build}" includeAntRuntime="no">
  30.             <classpath refid="test.classpath" />
  31.         </javac>
  32.     </target>
  33.     <target name="test" depends="compile" description="test the source " >
  34.         <!-- JUnitによるテストを実行 -->
  35.         <junit fork="yes" haltonfailure="yes">
  36.             <test name="${test.class.name}" />
  37.             <formatter type="plain" usefile="false" />
  38.             <classpath refid="test.classpath" />
  39.         </junit>
  40.     </target>
  41.     <target name="dist" depends="test" description="generate the distribution" >
  42.         <!-- jarファイルの出力先ディレクトリを作成 -->
  43.         <mkdir dir="${dist}"/>
  44.         <!-- sample-yyyymmdd.jarという名前でjarファイルを作成 -->
  45.         <jar jarfile="${dist}/sample-${DSTAMP}.jar" basedir="${build}">
  46.             <manifest>
  47.                 <attribute name="Main-Class" value="sample.Main" />
  48.             </manifest>
  49.         </jar>
  50.     </target>
  51.     <target name="clean" description="clean up" >
  52.         <!-- Delete the ${build} and ${dist} directory trees -->
  53.         <delete dir="${build}"/>
  54.         <delete dir="${test.build}"/>
  55.         <delete dir="${dist}"/>
  56.     </target>
  57. </project>



javacにincludeAntRuntime="no"をつけていますが、これがないと

warning: 'includeantruntime' was not set, defaulting to build.sysclasspath=last; set to false for repeatable builds


という警告が発生しました。

javac(ant) の警告
http://d.hatena.ne.jp/eggtoothcroc/20100526/p1
こちらを参考にさせていただいてます。





Eclipseのantプラグインで実行



antを実行するプラグインは、特にインストールした記憶が無いので、
初期状態で入っていると思います。

build.xmlを右クリックして実行でもよいのですが、
[ウィンドウ]-[ビューの表示]-[Ant]を実行します。

100_02.png


すると、antビューが表示されます。
ビルドファイルの追加ボタンをクリックし、対象のbuild.xmlを指定します。

100_03.png


好きなターゲットを選んで実行することができます。
distを選択して、「選択されたターゲットの実行」をクリック。

100_04.png


コンソールに実行結果が表示されます。
ちゃんとjunitによるテストも行われています。

100_05.png


distに出力されたjarファイルを見てみると、狙い通りMain.classだけ
含まれており、余分なMainTest.classは含まれていません。
※jarにするときに、ファイル名にTestがあるものは含めない。というフィルターでもよいかと。

100_06.png






JUnitエラー時の挙動



Mainクラスで足し算しているところを掛け算に変更し、テストがエラーになるようにしてみます。


  1.     //引数2つを加えるだけ
  2.     public static int add(int a, int b) {
  3.         //間違えて掛け算する
  4.         //return a + b;
  5.         return a * b;
  6.     }




BUILD FAILEDとなり、jarファイルは作成されません。

100_07.png


Buildfile: /home/workspace/junitsample/build.xml
init:
compile:
test:
    [junit] Testsuite: sample.MainTest
    [junit] Tests run: 1, Failures: 1, Errors: 0, Time elapsed: 0.011 sec
    [junit] Testcase: testAdd took 0.004 sec
    [junit]     FAILED
    [junit] Expected: is <15>
    [junit]     got: <50>
    [junit] junit.framework.AssertionFailedError:
    [junit] Expected: is <15>
    [junit]     got: <50>
    [junit]     at sample.MainTest.testAdd(Unknown Source)

BUILD FAILED
/home/workspace/junitsample/build.xml:40: Test sample.MainTest failed

Total time: 764 milliseconds






【参考URL】

JUnit FAQ - How do I run JUnit using Ant?
http://junit.sourceforge.net/doc/faq/faq.htm#running_5

Using Apache Ant - Example Buildfile
http://ant.apache.org/manual/index.html

javac(ant) の警告
http://d.hatena.ne.jp/eggtoothcroc/20100526/p1


関連記事

コメント

初めまして。

antビルドの話、勉強になります!

プロフィール

Author:symfo
blog形式だと探しにくいので、まとめサイト作成中です。
https://symfo.web.fc2.com/

PR

検索フォーム

月別アーカイブ