2010-06-09 27 views
1

我正在嘗試從日誌文件中寫入JUnite Suite失敗的測試總數。JUnit 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) { 
    } 
}; 

有關我應該如何做到這一行爲的任何想法?

回答

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

}