Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ignore Assertion failure in a testcase (JUnit)

Tags:

java

junit

qa

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

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

like image 559
user638297 Avatar asked Feb 28 '11 20:02

user638297


People also ask

What does fail () method do in JUnit?

The junit fail method is used to verify that the actual exception will throw an error or the test is failing at the time of development. The junit fail assertion will fails while throwing error of assertion which was unconditional.


2 Answers

You could use a 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)

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.

like image 75
Thomas Jung Avatar answered Sep 18 '22 22:09

Thomas Jung


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.

like image 22
Tomasz Nurkiewicz Avatar answered Sep 19 '22 22:09

Tomasz Nurkiewicz