在JUnit4中使用参数化测试时,是否有一种方法可以设置我自己的自定义测试用例名称?

我想改变默认的-[测试类]. runtest [n] -有意义的东西。


当前回答

我大量使用静态导入的Assert和朋友,所以我很容易重新定义断言:

private <T> void assertThat(final T actual, final Matcher<T> expected) {
    Assert.assertThat(editThisToDisplaySomethingForYourDatum, actual, expected);
}

例如,您可以向您的测试类添加一个“name”字段,在构造函数中初始化,并在测试失败时显示它。只需将它作为每个测试参数数组的第一个元素传递进来。这也有助于标记数据:

public ExampleTest(final String testLabel, final int one, final int two) {
    this.testLabel = testLabel;
    // ...
}

@Parameters
public static Collection<Object[]> data() {
    return asList(new Object[][]{
        {"first test", 3, 4},
        {"second test", 5, 6}
    });
}

其他回答

这个特性已经加入到JUnit 4.11中。

若要使用更改参数化测试的名称,请执行以下命令:

@Parameters(name="namestring")

Namestring是一个字符串,可以有以下特殊占位符:

{index} -这组参数的索引。默认名称字符串是{index}。 {0} -测试调用的第一个参数值。 {1} -第二个参数值 等等

测试的最终名称将是测试方法的名称,后面跟着括号中的名称字符串,如下所示。

例如(改编自Parameterized注释的单元测试):

@RunWith(Parameterized.class)
static public class FibonacciTest {

    @Parameters( name = "{index}: fib({0})={1}" )
    public static Iterable<Object[]> data() {
        return Arrays.asList(new Object[][] { { 0, 0 }, { 1, 1 }, { 2, 1 },
                { 3, 2 }, { 4, 3 }, { 5, 5 }, { 6, 8 } });
    }

    private final int fInput;
    private final int fExpected;

    public FibonacciTest(int input, int expected) {
        fInput= input;
        fExpected= expected;
    }

    @Test
    public void testFib() {
        assertEquals(fExpected, fib(fInput));
    }

    private int fib(int x) {
        // TODO: actually calculate Fibonacci numbers
        return 0;
    }
}

将给出像testFib[1: fib(1)=1]和testFib[4: fib(4)=3]这样的名称。(名称的testFib部分是@Test的方法名)。

我大量使用静态导入的Assert和朋友,所以我很容易重新定义断言:

private <T> void assertThat(final T actual, final Matcher<T> expected) {
    Assert.assertThat(editThisToDisplaySomethingForYourDatum, actual, expected);
}

例如,您可以向您的测试类添加一个“name”字段,在构造函数中初始化,并在测试失败时显示它。只需将它作为每个测试参数数组的第一个元素传递进来。这也有助于标记数据:

public ExampleTest(final String testLabel, final int one, final int two) {
    this.testLabel = testLabel;
    // ...
}

@Parameters
public static Collection<Object[]> data() {
    return asList(new Object[][]{
        {"first test", 3, 4},
        {"second test", 5, 6}
    });
}

我最近在使用JUnit 4.3.1时遇到了同样的问题。我实现了一个新的类,它扩展了Parameterized,称为LabelledParameterized。它已经使用JUnit 4.3.1、4.4和4.5进行了测试。它使用@Parameters方法中每个参数数组的第一个参数的String表示形式重新构造Description实例。你可以在这里看到代码:

http://code.google.com/p/migen/source/browse/trunk/java/src/.../LabelledParameterized.java?r=3789

下面是它在以下场合的用法:

http://code.google.com/p/migen/source/browse/trunk/java/src/.../ServerBuilderTest.java?r=3789

测试描述在Eclipse中的格式很好,这是我想要的,因为这使得失败的测试更容易找到!我可能会在接下来的几天/几周内进一步完善和记录这些类。去掉'?'部分的url,如果你想要最先进的。:-)

要使用它,您所要做的就是复制这个类(GPL v3),并假设参数列表的第一个元素是一个合理的标签,将@RunWith(Parameterized.class)更改为@RunWith(LabelledParameterized.class)。

我不知道JUnit的后续版本是否解决了这个问题,但即使他们解决了,我也不能更新JUnit,因为我的所有合作开发人员也必须更新,我们有比重新工具更重要的优先级。因此,类中的工作可以被多个版本的JUnit编译。


注意:这里有一些反射花招,这样它就可以在上面列出的不同JUnit版本之间运行。针对JUnit 4.3.1的版本可以在这里找到,针对JUnit 4.4和4.5的版本可以在这里找到。

使用Parameterized作为模型,我编写了自己的自定义测试运行器/套件——只花了大约半小时。它与darrenp的LabelledParameterized略有不同,因为它允许您显式地指定名称,而不是依赖于第一个参数的toString()。

它也不使用数组,因为我讨厌数组。:)

public class PolySuite extends Suite {

  // //////////////////////////////
  // Public helper interfaces

  /**
   * Annotation for a method which returns a {@link Configuration}
   * to be injected into the test class constructor
   */
  @Retention(RetentionPolicy.RUNTIME)
  @Target(ElementType.METHOD)
  public static @interface Config {
  }

  public static interface Configuration {
    int size();
    Object getTestValue(int index);
    String getTestName(int index);
  }

  // //////////////////////////////
  // Fields

  private final List<Runner> runners;

  // //////////////////////////////
  // Constructor

  /**
   * Only called reflectively. Do not use programmatically.
   * @param c the test class
   * @throws Throwable if something bad happens
   */
  public PolySuite(Class<?> c) throws Throwable {
    super(c, Collections.<Runner>emptyList());
    TestClass testClass = getTestClass();
    Class<?> jTestClass = testClass.getJavaClass();
    Configuration configuration = getConfiguration(testClass);
    List<Runner> runners = new ArrayList<Runner>();
    for (int i = 0, size = configuration.size(); i < size; i++) {
      SingleRunner runner = new SingleRunner(jTestClass, configuration.getTestValue(i), configuration.getTestName(i));
      runners.add(runner);
    }
    this.runners = runners;
  }

  // //////////////////////////////
  // Overrides

  @Override
  protected List<Runner> getChildren() {
    return runners;
  }

  // //////////////////////////////
  // Private

  private Configuration getConfiguration(TestClass testClass) throws Throwable {
    return (Configuration) getConfigMethod(testClass).invokeExplosively(null);
  }

  private FrameworkMethod getConfigMethod(TestClass testClass) {
    List<FrameworkMethod> methods = testClass.getAnnotatedMethods(Config.class);
    if (methods.isEmpty()) {
      throw new IllegalStateException("@" + Config.class.getSimpleName() + " method not found");
    }
    if (methods.size() > 1) {
      throw new IllegalStateException("Too many @" + Config.class.getSimpleName() + " methods");
    }
    FrameworkMethod method = methods.get(0);
    int modifiers = method.getMethod().getModifiers();
    if (!(Modifier.isStatic(modifiers) && Modifier.isPublic(modifiers))) {
      throw new IllegalStateException("@" + Config.class.getSimpleName() + " method \"" + method.getName() + "\" must be public static");
    }
    return method;
  }

  // //////////////////////////////
  // Helper classes

  private static class SingleRunner extends BlockJUnit4ClassRunner {

    private final Object testVal;
    private final String testName;

    SingleRunner(Class<?> testClass, Object testVal, String testName) throws InitializationError {
      super(testClass);
      this.testVal = testVal;
      this.testName = testName;
    }

    @Override
    protected Object createTest() throws Exception {
      return getTestClass().getOnlyConstructor().newInstance(testVal);
    }

    @Override
    protected String getName() {
      return testName;
    }

    @Override
    protected String testName(FrameworkMethod method) {
      return testName + ": " + method.getName();
    }

    @Override
    protected void validateConstructor(List<Throwable> errors) {
      validateOnlyOneConstructor(errors);
    }

    @Override
    protected Statement classBlock(RunNotifier notifier) {
      return childrenInvoker(notifier);
    }
  }
}

举个例子:

@RunWith(PolySuite.class)
public class PolySuiteExample {

  // //////////////////////////////
  // Fixture

  @Config
  public static Configuration getConfig() {
    return new Configuration() {
      @Override
      public int size() {
        return 10;
      }

      @Override
      public Integer getTestValue(int index) {
        return index * 2;
      }

      @Override
      public String getTestName(int index) {
        return "test" + index;
      }
    };
  }

  // //////////////////////////////
  // Fields

  private final int testVal;

  // //////////////////////////////
  // Constructor

  public PolySuiteExample(int testVal) {
    this.testVal = testVal;
  }

  // //////////////////////////////
  // Test

  @Ignore
  @Test
  public void odd() {
    assertFalse(testVal % 2 == 0);
  }

  @Test
  public void even() {
    assertTrue(testVal % 2 == 0);
  }

}

查看dsaff提到的JUnitParams,它使用ant在html报告中构建参数化测试方法描述。

这是在尝试LabelledParameterized并发现它虽然可以与eclipse一起工作,但就html报告而言,它不能与ant一起工作。

欢呼,