【问题标题】:Log information inside a JUnit SuiteJUnit Suite 中的日志信息
【发布时间】:2011-03-01 15:38:42
【问题描述】:

我目前正尝试在日志文件中写入来自 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) {
    }
};

关于我应该做些什么来实现这种行为有什么想法吗?

【问题讨论】:

    标签: java unit-testing junit test-suite


    【解决方案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();
     }
    
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-06-26
      • 2018-10-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多