忽略测试用例中的断言失败(JUnit) [英] Ignore Assertion failure in a testcase (JUnit)

查看:140
本文介绍了忽略测试用例中的断言失败(JUnit)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当前,我正在使用Java和Selenium rc编写自动化测试.

Currently, I am writing the automation testing using java and selenium rc.

我想验证用户界面上显示的所有内容,功能如下:

I would like to verify all the contents present on the user interface, the function is ie below:

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;
}

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

the function suppose return a String contains information about the testing. However, the function stopped once there is an assertion error happened.

因此,我想知道是否存在一种忽略故障并继续执行所有断言验证的方法.

So, I want to know whether there is a way to ignore the failure and keep executing all the assertion verifications.

感谢您的帮助

推荐答案

您可以使用

ErrorCollector规则允许 执行测试以在之后继续 找到第一个问题(对于 例如,收集 all 表中不正确的行,并报告 全部一次)

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)

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

For example you can write a test like this.

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.根据您的喜好,这是否肯定.

The error collector uses hamcrest Matchers. Depending on your preferences this is positive or not.

这篇关于忽略测试用例中的断言失败(JUnit)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆