的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