目前,我正在使用 java 和 selenium rc 编写自动化测试。
我想验证用户界面上的所有内容,功能如下:
public String UITest() throws IOException {
String result="Test Start<br />";
try {
openfile(1);
for (String url : uiMaps.keySet()) {
selenium.open(url);
for (String item : uiMaps.get(url)) {
assertEquals(url+" check: " + item, true,selenium.isTextPresent(item));
result+=url+" check: " + item+" : OK<br />";
}
}
} catch (AssertionError e) {
result+=e.getMessage();
}
result+="Test finished<br />";
return result;
}
该函数应该返回一个包含测试信息的字符串。但是,一旦发生断言错误,该函数就会停止。
有没有办法忽略失败并继续执行所有断言验证?
最佳答案
你可以使用 JUnit 4 error collector rule :
The ErrorCollector rule allows execution of a test to continue after the first problem is found (for example, to collect all the incorrect rows in a table, and report them all at once)
例如,您可以编写这样的测试。
public static class UsesErrorCollectorTwice {
@Rule
public ErrorCollector collector= new ErrorCollector();
@Test
public void example() {
String x = [..]
collector.checkThat(x, not(containsString("a")));
collector.checkThat(y, containsString("b"));
}
}
错误收集器使用 hamcrest Matchers。这是积极的还是消极的,取决于您的偏好。
关于java - 忽略测试用例中的断言失败 (JUnit),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5147187/