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