我目前正在尝试在日志文件中写入JUnite Suite中失败测试的总数。
我的测试套件定义如下:
@RunWith(Suite.class)
@SuiteClasses({Class1.class, Class2.class etc.})
public class SimpleTestSuite {}
我试图定义一个规则,当测试失败时会增加错误总数,但显然我的规则永远不会被调用。
@Rule
public MethodRule logWatchRule = new TestWatchman() {
public void failed(Throwable e, FrameworkMethod method) {
errors += 1;
}
public void succeeded(FrameworkMethod method) {
}
};
关于我应该采取哪些措施来实现这种行为?
答案 0 :(得分:1)
以下代码适用于我。我怀疑你没有宣称errors
是静态的。
在运用每种测试方法之前,请考虑JUnit创建一个new instance的夹具。
public class WatchmanExample {
private static int failedCount = 0;
@Rule
public MethodRule watchman = new TestWatchman() {
@Override
public void failed(Throwable e, FrameworkMethod method) {
failedCount++;
}
};
@Test
public void test1() {
fail();
}
@Test
public void test2() {
fail();
}
}