开发者

Log information inside a JUnit Suite

开发者 https://www.devze.com 2023-01-03 01:21 出处:网络
I\'m currently trying to write inside a log file the tot开发者_如何学Pythonal number of failed tests from a JUnite Suite.

I'm currently trying to write inside a log file the tot开发者_如何学Pythonal number of failed tests from a JUnite Suite.

My testsuite is defined as follows:

@RunWith(Suite.class)
@SuiteClasses({Class1.class, Class2.class etc.})
public class SimpleTestSuite {}

I tried to define a rule which would increase the total number of errors when a test fails, but apparently my rule is never called.

@Rule
public MethodRule logWatchRule = new TestWatchman() {
    public void failed(Throwable e, FrameworkMethod method) {
         errors += 1;
    }

    public void succeeded(FrameworkMethod method) {
    }
};

Any ideas on what I should to do to achieve this behaviour?


The following code works for me. I suspect you are not declaring errors as static.

Consider that JUnit creates a new instance of the fixture before exercising each test method.

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();
 }

}
0

精彩评论

暂无评论...
验证码 换一张
取 消