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

标签 java junit

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

相关文章:

java - 如何将 java ArrayList 转换为数组?

java - Maven:将生成的 tomcat-maven-plugin 资源放在哪里?

java - LifecycleOwner onChanged 永远循环

mysql - Play Framework 2.5 - 不使用内存数据库进行测试

java - Spring Boot 应用程序无法加载测试属性

java - Spring boot Resource getFile() 不起作用

java - Selenium Web Driver 似乎间歇性地跳过 driver.get()

java - JUnit 如何测试 ParseException

groovy - 正确的位置(在Gradle中)放置应在_all_测试用例中运行的代码?

java - i/p 字符串错误