JUnit 4.11 不显示参数值但显示索引
JUnit 4.11 not showing parameter value but index
我们有一个包含许多 JUnit 测试的项目classes,直到最近一直在使用 Eclipse 内部 JUnit 实现。
为了 运行 我们从 ant 构建脚本进行测试,我们更改了项目的构建路径以使用外部 junit-4.11.jar 和所需的 hamcrest-core 库。
我们的一些测试 classes 在 @Parameters 注释中使用带有 (name = "{0}") 选项的参数化 JUnit Runner。 运行使用 Eclipse 内置 JUnit classing 这些测试时,输出显示第一个参数的值而不仅仅是索引。
现在,在使用外部 JUnit 后,无论我 运行 从 Eclipse 内部测试还是使用 "test" 目标的 ant 构建脚本,都只显示索引。
这是一个测试 class:
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") 内部 运行 这个 class 我得到 "initializationError" 并且失败跟踪显示 "java.lang.Exception: Test class should have exactly one public zero-argument constructor"。
如果我 运行 带有 "test" 的 ant 构建脚本中的测试以测试 运行 为目标而没有错误,但输出显示 "test[0]" 和 "test[1]" 而不是 "test[name1]" 和 "test[name2]".
总结一下:
1. 如果我向测试 class 添加一个构造函数,并根据需要添加尽可能多的参数,它不会从 Eclipse 中 运行 。
2. 如果没有构造函数,Eclipse 中的测试 运行s 和测试的命名是从配置的参数中正确获取的。但是,如果没有构造函数,ant 脚本中的测试将不会 运行。
目标是来自 Eclipse 和 ant 脚本的测试 运行 以及它们在两种情况下都显示正确的名称。
编辑2:
根据文档,您可以使用 @Parameter 注入参数,也可以使用构造函数。当我编辑上面的测试 class 并删除 @Parameter 注释时,它在 Eclipse 中工作正常,并在每个 运行 旁边显示正确的名称。但是,当来自 ant 脚本的 运行 时,它仍然 运行 没问题,但不显示名称,而是显示测试旁边的索引位置。
重要的是将正确的 JUnit 版本(在本例中为 4.11)添加到 JUnit ant 任务的类路径中。这是一个编译和执行 ParameterTest 的简单 ant 文件。目录结构如下所示:
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" />
我们有一个包含许多 JUnit 测试的项目classes,直到最近一直在使用 Eclipse 内部 JUnit 实现。 为了 运行 我们从 ant 构建脚本进行测试,我们更改了项目的构建路径以使用外部 junit-4.11.jar 和所需的 hamcrest-core 库。 我们的一些测试 classes 在 @Parameters 注释中使用带有 (name = "{0}") 选项的参数化 JUnit Runner。 运行使用 Eclipse 内置 JUnit classing 这些测试时,输出显示第一个参数的值而不仅仅是索引。 现在,在使用外部 JUnit 后,无论我 运行 从 Eclipse 内部测试还是使用 "test" 目标的 ant 构建脚本,都只显示索引。 这是一个测试 class:
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") 内部 运行 这个 class 我得到 "initializationError" 并且失败跟踪显示 "java.lang.Exception: Test class should have exactly one public zero-argument constructor"。 如果我 运行 带有 "test" 的 ant 构建脚本中的测试以测试 运行 为目标而没有错误,但输出显示 "test[0]" 和 "test[1]" 而不是 "test[name1]" 和 "test[name2]".
总结一下: 1. 如果我向测试 class 添加一个构造函数,并根据需要添加尽可能多的参数,它不会从 Eclipse 中 运行 。 2. 如果没有构造函数,Eclipse 中的测试 运行s 和测试的命名是从配置的参数中正确获取的。但是,如果没有构造函数,ant 脚本中的测试将不会 运行。
目标是来自 Eclipse 和 ant 脚本的测试 运行 以及它们在两种情况下都显示正确的名称。
编辑2: 根据文档,您可以使用 @Parameter 注入参数,也可以使用构造函数。当我编辑上面的测试 class 并删除 @Parameter 注释时,它在 Eclipse 中工作正常,并在每个 运行 旁边显示正确的名称。但是,当来自 ant 脚本的 运行 时,它仍然 运行 没问题,但不显示名称,而是显示测试旁边的索引位置。
重要的是将正确的 JUnit 版本(在本例中为 4.11)添加到 JUnit ant 任务的类路径中。这是一个编译和执行 ParameterTest 的简单 ant 文件。目录结构如下所示:
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" />