忽略测试用例中的断言失败(JUnit)

use*_*297 14 java junit qa

目前,我正在使用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;
}
Run Code Online (Sandbox Code Playgroud)

函数假设返回一个String包含有关测试的信息.但是,一旦发生断言错误,该函数就会停止.

所以,我想知道是否有办法忽略失败并继续执行所有断言验证.

谢谢你的帮助

Tho*_*ung 18

您可以使用JUnit 4错误收集器规则:

ErrorCollector规则允许在找到第一个问题后继续执行测试(例如,收集表中的所有不正确的行,并立即报告所有行)

例如,您可以编写这样的测试.

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"));             
  }
}
Run Code Online (Sandbox Code Playgroud)

错误收集器使用hamcrest Matchers.根据您的喜好,这是积极的与否.