我目前正在尝试在日志文件中写入 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) {
}
};
关于我应该做什么来实现这种行为有什么想法吗?
最佳答案
以下代码对我有用。我怀疑您没有将错误
声明为静态。
考虑 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();
}
}
关于java - JUnit Suite 内的日志信息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3005787/