0

我正在尝试创建一个 junit Runner,它将使用junit API将常见测试组合在一起:

package whatever;

import org.junit.runner.Description;
import org.junit.runner.Runner;
import org.junit.runner.notification.Failure;
import org.junit.runner.notification.RunNotifier;

public class SomeTestRunner extends Runner {

    public SomeTestRunner(Class<?> testClass) {}

    @Override
    public Description getDescription() {
        return Description.EMPTY;
    }

    @Override
    public void run(RunNotifier notifier) {
        for (int i = 0; i < 3; i++) {
            Description parent = Description.createSuiteDescription("Parent_" + i);
            for (int j = 0; j < 3; j++) {
                Description child = Description.createTestDescription(Exception.class, "Child_" + j);
                parent.addChild(child);
                Failure failure = new Failure(child, new Exception());
                notifier.fireTestFailure(failure);
            }
            Failure failure = new Failure(parent, new Exception());
            notifier.fireTestFailure(failure);
        }
    }
}

问题是,当我使用这个 Runner 运行测试时,我可以看到连续的失败,包括父母和孩子,而不是组合在一起:

Results :

Tests in error: 
  Child_0(java.lang.Exception)
  Child_1(java.lang.Exception)
  Child_2(java.lang.Exception)
  Parent_0
  Child_0(java.lang.Exception)
  Child_1(java.lang.Exception)
  Child_2(java.lang.Exception)
  Parent_1
  Child_0(java.lang.Exception)
  Child_1(java.lang.Exception)
  Child_2(java.lang.Exception)
  Parent_2

Tests run: 12, Failures: 0, Errors: 12, Skipped: 0

此外,当我在 Eclipse 中运行此测试时,我希望看到它们组合在一起 - 但事实并非如此。我错过了什么?甚至可能吗?

4

2 回答 2

0

要遵循的模板是Parameterized,因为它似乎可以做你想做的事。对于给定的测试类,它多次运行测试方法,它为每组参数创建一个Runner ,这就是我认为你想要的:

public class GroupedTestRunner extends Suite {
  private class TestClassRunnerForParameters extends BlockJUnit4ClassRunner {
    private String name;

    TestClassRunnerForParameters(Class<?> type, String name) throws InitializationError {
      super(type);
      this.name = name;
    }

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

    @Override
    protected String getName() {
      return String.format("[%s]", name);
    }

    @Override
    protected String testName(final FrameworkMethod method) {
      return String.format("%s[%s]", method.getName(), name);
    }
  }

  private final ArrayList<Runner> runners = new ArrayList<Runner>();

  public GroupedTestRunner(Class<?> klass) throws Throwable {
    super(klass, Collections.<Runner> emptyList());

    // do grouping things here
    runners.add(new TestClassRunnerForParameters(getTestClass().getJavaClass(), "group1"));
    runners.add(new TestClassRunnerForParameters(getTestClass().getJavaClass(), "group2"));
  }

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

这会产生类似的输出(在 Eclipse 中):

在此处输入图像描述

于 2012-02-22T00:32:34.663 回答
0

您可以使用 JUnit 测试套件,就像这样:

import org.junit.runner.RunWith;
import org.junit.runners.Suite;
import org.junit.runners.Suite.SuiteClasses;

@RunWith(Suite.class)
@SuiteClasses({ SomeTest.class, AnotherTest.class, YetAnotherTest.class })
public class AllTests {

}
于 2012-02-21T21:44:22.870 回答