的JUnit有一個名爲ErrorCollector該CA規則n用於在執行結束時收集關於測試和報告的漸進統計信息。
不結合斷言和ErrorCollector,你會如果你的測試從聲明
未能在測試結束任何故障將被收集並在JUnit輸出集中上市失去ErrorCollector信息。
package stackoverflow.proof.junit;
import org.hamcrest.core.Is;
import org.hamcrest.core.IsEqual;
import org.hamcrest.core.IsNull;
import org.junit.Assert;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ErrorCollector;
/**
* Test case for demonstrating some Junit Rule behaviors.
* <p/>
* Requires at least JUnit 4.7
*
* @see "http://stackoverflow.com/questions/33183789/not-able-to-continue-when-an-assert-condition-fails-in-selenium-webdriver/33183882#33183882"
* @author http://stackoverflow.com/users/5407189/jeremiah
* @since Oct 17, 2015
*
*/
public class JUnitRulesDemoTest {
/**
* Collector which can be used to track problems through an event series and still allow a test to run to
* completion.
*/
@Rule
public ErrorCollector collector = new ErrorCollector();
/**
* Test illustrating ErrorCollector behavior.
*
*/
@Test
public void testErrorCollector() {
collector.checkThat("These things don't match", new Object(), IsEqual.equalTo(new Object()));
collector.checkThat(new Object(), IsNull.notNullValue());
try {
throw new Exception("Demonstration Exception in collector");
} catch (Exception ex) {
collector.addError(ex);
}
collector.checkThat("Status does not match!", true, Is.is(true));
System.out.println("Done.");
}
/**
* Test illustrating ErrorCollector behavior when an Assert Fails the Test.
*
*/
@Test
public void testErrorCollectorWithAssert() {
collector.checkThat("These things don't match", new Object(), IsEqual.equalTo(new Object()));
collector.checkThat(new Object(), IsNull.notNullValue());
try {
throw new Exception("Demonstration Exception in collector");
} catch (Exception ex) {
collector.addError(ex);
}
//Test stops here. You won't see "done", nor will you see any of the previous ErrorCollector status'
Assert.assertTrue(false);
collector.checkThat("Status does not match!", true, Is.is(true));
System.out.println("Done.");
}
}
規則必須:
建議的解決方案:
在你列出你上面應該刪除try/catch塊,並使用是匹配器,而不是org.junit.Assert.assertEquals的情況下:
@Rule
public ErrorCollector collector = new ErrorCollector();
@Test
public void testSomething() {
boolean status = doStatusEval();
collector.checkThat("Disabled user can register for workshop", false, Is.is(status));
//Continue adding things to the collector.
}
祝你好運!
在Junit截圖中,我應該指出兩個junit測試是在同一個會話中運行的。 * testErrorCollectorWithAssert *的證明在控制檯中被調用,您只能在兩個測試中看到一個「完成」語句。其中只有一個完成。 – Jeremiah