如何为参数化的jUnit测试用例编写ant脚本?

时间:2013-12-17 18:43:27

标签: ant junit parameterized

我已编写参数化测试用例。当我通过蚂蚁运行它时,我没有得到理想的结果。如果没有Parameterized,测试用例将按需运行。请告诉我我错在哪里以及我需要做出哪些改变。

代码:

import java.util.Arrays;
import java.util.Collection;
import junit.framework.TestCase;
import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import org.junit.runners.Parameterized.Parameters;

@RunWith(value = Parameterized.class)
public class HelloWorldTest extends TestCase {

    int num1;
    int num2;
    int num3;

    public HelloWorldTest(int num1, int num2, int num3) {
        super();
        this.num1 = num1;
        this.num2 = num2;
        this.num3 = num3;
    }

    @Parameters
    public static Collection<Object[]> data() {
        Object[][] data = new Object[][] { { 1, 1, 2 }, { 2, 2, 5 } };
        return Arrays.asList(data);
    }

    @Test
    public void testSummationPositive() {
        Assert.assertEquals(num1 + num2, num3);
    }

}

Ant脚本:

<?xml version="1.0" encoding="UTF-8"?>
<project name="HelloWorld" basedir="." default="main">

    <property name="src.dir" value="src" />
    <property name="test.dir" value="test" />
    <property name="build.dir" value="build" />
    <property name="classes.dir" value="${build.dir}/classes" />
    <property name="jar.dir" value="${build.dir}/jar" />

    <property name="main-class" value="HelloWorld" />

    <property name="lib.dir" value="lib" />
    <path id="classpath">
        <fileset dir="${lib.dir}" includes="**/*.jar" />
    </path>

    <path id="application" location="${jar.dir}/${ant.project.name}.jar" />

    <target name="clean">
        <delete dir="${build.dir}" />
    </target>

    <target name="compile">
        <mkdir dir="${classes.dir}" />
        <javac srcdir="${src.dir}" destdir="${classes.dir}"
            classpathref="classpath" />
    </target>

    <target name="test-compile">
        <mkdir dir="${classes.dir}" />
        <javac srcdir="${test.dir}" destdir="${classes.dir}"
            classpathref="classpath" />
    </target>

    <target name="jar" depends="compile,test-compile">
        <mkdir dir="${jar.dir}" />
        <jar destfile="${jar.dir}/${ant.project.name}.jar" basedir="${classes.dir}">
            <manifest>
                <attribute name="Main-Class" value="${main-class}" />
            </manifest>
        </jar>
    </target>

    <target name="run" depends="jar,junit">
        <java classname="${main-class}" fork="true">
            <classpath>
                <path refid="classpath" />
                <path refid="application" />
                <path location="${jar.dir}/${ant.project.name}.jar"></path>
            </classpath>
        </java>
    </target>

    <target name="junit" depends="jar">
        <junit printsummary="yes">
            <classpath>
                <path refid="classpath" />
                <path refid="application" />
            </classpath>
            <batchtest fork="yes">
                <fileset dir="${test.dir}" includes="*Test.java" />
            </batchtest>
        </junit>
    </target>

    <target name="clean-build" depends="clean,jar" />

    <target name="main" depends="clean,run" />
</project>

1 个答案:

答案 0 :(得分:2)

由于您使用的是JUnit 4,因此不需要extends TestCase声明。尝试删除它,看看你的测试是否正常运行。