如何将JUnit测试组合在一起

时间:2012-02-21 21:30:07

标签: java junit junit-runner

我正在尝试创建一个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中运行此测试时,我希望看到它们组合在一起 - 但事实并非如此。我错过了什么?它甚至可能吗?

2 个答案:

答案 0 :(得分:0)

您可以使用JUnit Test Suite,就像这样:

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 {

}

答案 1 :(得分: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中)的输出:

enter image description here