java - JUnit Suite 内的日志信息

标签 java unit-testing junit test-suite

我目前正在尝试在日志文件中写入 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/

相关文章:

java - 运行 ant junit 时出现 ClassNotFoundException

java - Java 控制台应用程序中的 Unicode 输入

unit-testing - 如何在 Visual Studio Code 中使用 Typescript 进行 TDD?

java - Java 中的 boolean 字段输出不正确

unit-testing - 框架如何发挥单元测试 Controller 方法

python - 单元测试测试顺序

unit-testing - Grails test-app:如何禁用junitreport?

unit-testing - Android Studio + Spek 集成

java - 异步加载 JButton 图标

java - 单击后退按钮会导致强制关闭/NullPointerException