java 忽略测试用例中的断言失败 (JUnit)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5147187/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Ignore Assertion failure in a testcase (JUnit)
提问by user638297
Currently, I am writing the automation testing using java and selenium rc.
目前,我正在使用 java 和 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;
}
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.
所以,我想知道是否有办法忽略失败并继续执行所有断言验证。
Thanks for any help
谢谢你的帮助
回答by Thomas Jung
You could use a JUnit 4 error collector rule:
您可以使用JUnit 4 错误收集器规则:
The ErrorCollector rule allows execution of a test to continue after the first problem is found (for example, to collect allthe incorrect rows in a table, and report them all at once)
ErrorCollector 规则允许在发现第一个问题后继续执行测试(例如,收集表中所有不正确的行,并一次性报告它们)
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"));
}
}
The error collector uses hamcrest Matchers. Depending on your preferences this is positive or not.
错误收集器使用 hamcrest 匹配器。根据您的喜好,这是积极的还是消极的。
回答by Tomasz Nurkiewicz
From Selenium documentation:
从硒文档:
All Selenium Assertions can be used in 3 modes: "assert", "verify", and "waitFor". For example, you can "assertText", "verifyText" and "waitForText". When an "assert" fails, the test is aborted. When a "verify" fails, the test will continue execution, logging the failure. This allows a single "assert" to ensure that the application is on the correct page, followed by a bunch of "verify" assertions to test form field values, labels, etc.
所有 Selenium 断言都可以在 3 种模式下使用:“assert”、“verify”和“waitFor”。例如,您可以“assertText”、“verifyText”和“waitForText”。当“断言”失败时,测试将中止。当“验证”失败时,测试将继续执行,记录失败。这允许单个“断言”来确保应用程序在正确的页面上,然后是一堆“验证”断言来测试表单字段值、标签等。
回答by mazaneicha
I'm sure you've figured it out yourself by now: the try-catch should be inside the for loop, not outside of it ;)
我相信你现在自己已经弄明白了:try-catch 应该在 for 循环内,而不是在它之外;)
回答by iluxa
Don't assert anything in the function. Return say null instead, and have whoever's calling it to keep going but then fail if the function returned null
不要在函数中断言任何东西。改为返回 null,并让调用它的人继续运行,但如果函数返回 null 则失败