【问题标题】:JUnit 4.11 not showing parameter value but indexJUnit 4.11不显示参数值但索引
【发布时间】:2015-08-14 12:00:29
【问题描述】:

我们有一个项目,其中包含许多 JUnit 测试类,直到最近它一直在使用 Eclipse 内部 JUnit 实现。 为了从 ant 构建脚本运行我们的测试,我们更改了项目的构建路径以使用外部 junit-4.11.jar 和所需的 hamcrest-core 库。 我们的一些测试类在 @Parameters 注释中使用带有 (name = "{0}") 选项的参数化 JUnit Runner。在使用 Eclipse 内置 JUnit 运行这些测试类时,输出显示第一个参数的值而不仅仅是索引。 现在,在使用外部 JUnit 之后,无论我是从 Eclipse 内部运行测试还是使用“测试”目标运行 ant 构建脚本,都只显示索引。 这是一个测试类:

package foo;

import java.util.Arrays;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import org.junit.runners.Parameterized.Parameter;
import org.junit.runners.Parameterized.Parameters;

@RunWith(Parameterized.class)
public class ParameterTest {

    public ParameterTest(String _name, String _value)
    {
        name = _name;
        value = _value;
    }   

    @Parameters(name = "{0}")
    public static Iterable<String[]> testData() {
        return Arrays.asList(new String[][] {
                { "name1", "value1" },
                { "name2", "value2" } });
    }

    @Parameter(0)
    public String name;

    @Parameter(1)
    public String value;

    @Test
    public void test() {
        System.out.println(name+value);
    }
}

必须添加构造函数才能从 ant 脚本运行测试,否则会导致 IllegalArgumentException。 可以删除 @Parameter(x) 注释,但这不会改变结果。

编辑: 如果我从 Eclipse 内部运行这个类(“Run As -> JUnit Test”),我会得到“initializationError”,并且失败跟踪显示“java.lang.Exception:测试类应该只有一个公共零参数构造函数”。 如果我从带有“test”目标的 ant 构建脚本中运行测试,则测试运行没有错误,但输出显示“test[0]”和“test[1]”而不是“test[name1]”和“test[名称2]"。

总结一下: 1. 如果我向测试类添加了一个构造函数,其参数与我需要的一样多,它将不会在 Eclipse 中运行。 2. 在没有构造函数的情况下,测试从 Eclipse 中运行,并且测试的命名正确地取自配置的参数。但是,如果没有构造函数,测试将无法从 ant 脚本运行。

目的是测试在 Eclipse 和 ant 脚本中运行,并且它们在两种情况下都显示正确的名称。

编辑2: 根据文档,您可以使用 @Parameter 注入参数,也可以使用构造函数。当我编辑上面的测试类并删除 @Parameter 注释时,它在 Eclipse 中工作正常,并在每次运行旁边显示正确的名称。但是,当从 ant 脚本运行时,它仍然可以正常运行,但不显示名称,而是显示测试旁边的索引位置。

【问题讨论】:

  • 您能否说明在运行该测试类时会发生什么以及您期望什么?
  • @Parameter 是什么?它未列在Parameterizedjavadoc 的嵌套类中。
  • JUnit 内容中的类是错误的,您可以使用无构造函数和 @Parameter(x) 注释,或者使用带有参数名称且没有参数注释的构造函数。当您在执行 ant 时遇到问题时,这适用于 Eclipse 和 IntelliJ,请发布您的构建脚本以及如何构建您的 ant 类路径。我想这就是问题所在。
  • 用 ant 脚本测试,它在 xml 文件中生成以下行 ` ` 所以它似乎在 ant 中工作。

标签: java eclipse ant junit


【解决方案1】:

重要的是将正确的 JUnit 版本(在本例中为 4.11)添加到 JUnit ant 任务的类路径中。这是一个简单的 ant 文件编译和执行 ParameterTest。目录结构如下:

src
   /test/ParameterTest.java
lib
   /junit-4.11.jar
   /hamcrest-core-1.3.jar

build.xml:

<?xml version="1.0" encoding="UTF-8"?>

<project name="test">
<property name="src" value="src" />
<property name="build" value="target/classes" />
<property name="lib" value="lib" />
<property name="test.result" value="target/test-results" />
<property name="java.version" value="1.8" />


<target name="init">
    <mkdir dir="target/test-results" />
    <mkdir dir="target/classes" />
</target>

<target name="compile" depends="init">
    <javac srcdir="src" destdir="${build}" listfiles="no" deprecation="off" debug="on" nowarn="on"
           includeantruntime="true" source="${java.version}" target="${java.version}">
        <classpath>
            <fileset dir="${lib}" includes="*.jar"/>
        </classpath>
    </javac>
</target>

<target name="test"  depends="compile">


<junit printsummary="on" haltonfailure="no" showoutput="no"
       fork="yes"
       forkmode="once" tempdir="/tmp">

    <formatter type="xml"/>

    <classpath>
        <!-- adding first the classpath inherited from the shell -->
        <pathelement path="${java.class.path}"/>
        <!-- add lib folder with junit-4.11.jar -->
        <fileset dir="${lib}" includes="*.jar"/>
    </classpath>

    <classpath location="${build}" />

    <batchtest todir="${test.result}">
        <!-- location of your compiled Junit classes -->
        <fileset dir="${src}">
            <include name="**/*Test*.java" />
        </fileset>
    </batchtest>
</junit>
</target>

使用 ant test 执行后,您可以查看 target/test-results/TEST-test.ParameterTest.xml 并看到以下几行:

  <testcase classname="test.ParameterTest" name="test[name1]" time="0.0"/>
  <testcase classname="test.ParameterTest" name="test[name2]" time="0.0" />

【讨论】:

  • 我能够从你的那里修复我的 build.xml。似乎与类路径顺序有关。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-09-20
  • 2012-06-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多