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

2022-09-04 01:26:09

目前,我正在使用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;
}

该函数应返回一个包含有关测试的信息的字符串。但是,一旦发生断言错误,该函数就会停止。

有没有办法忽略失败并继续执行所有断言验证?


答案 1

您可以使用 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"));             
  }
}

错误收集器使用 hamcrest Matchers。根据您的喜好,这是积极的还是不积极的。


答案 2

来自硒文档

所有硒断言都可以在3种模式下使用:“断言”,“验证”和“等待”。例如,您可以“assertText”,“verifyText”和“waitForText”。当“断言”失败时,测试将中止。当“验证”失败时,测试将继续执行,并记录失败。这允许单个“断言”来确保应用程序位于正确的页面上,然后是一堆“验证”断言来测试表单字段值,标签等。


推荐