views:

45

answers:

1

I'm currently trying to write inside a log file the total 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?

+1  A: 

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

}
Ash Kim