[英]Log all errors/failures from a list of JUnit tests into a text file
我正在編寫一個測試工具和一組junit測試,以在各種環境下測試各種HTTP方法。 目前,我編寫了許多測試,最終我想要做的是能夠將Junit測試中的所有錯誤/失敗輸出到文本文件。 做到這一點的最佳方法是什么?
例如,如果測試失敗,我想說一下junit測試的名稱和一些信息(這些信息可能來自Test Harness類,例如響應狀態代碼和狀態代碼描述)。
嘗試使用Apache Log4J 。
另一種方法是將信息發送到文件。 檢查本教程 。 如果測試失敗,則只需append()
信息append()
到文件中即可。
.append(this.class() + respone.getStatus() + response.getCode());
您應該使用https://github.com/junit-team/junit/wiki/Rules#testwatchmantestwatcher-rules上詳細介紹的TestWatcher
public class WatchmanTest {
private static String watchedLog;
@Rule
public TestRule watchman = new TestWatcher() {
@Override
public Statement apply(Statement base, Description description) {
return super.apply(base, description);
}
@Override
protected void succeeded(Description description) {
watchedLog += description.getDisplayName() + " " + "success!\n";
}
@Override
protected void failed(Throwable e, Description description) {
watchedLog += description.getDisplayName() + " " + e.getClass().getSimpleName() + "\n";
}
@Override
protected void skipped(AssumptionViolatedException e, Description description) {
watchedLog += description.getDisplayName() + " " + e.getClass().getSimpleName() + "\n";
}
@Override
protected void starting(Description description) {
super.starting(description);
}
@Override
protected void finished(Description description) {
super.finished(description);
}
};
@Test
public void fails() {
fail();
}
@Test
public void succeeds() {
}
}
您將實現這些方法,並且JUnit為您的failed()方法提供了一個回調,除了導致失敗的異常和一個Description對象,該對象包含有關測試的更多信息。 您還應該查看該頁面上的ExternalResource類,因為它詳細說明了如何進行可靠的設置以及拆卸要在TestWatcher中使用的資源(例如要寫入的文件)的方法。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.