Can an Assertion be set up such that even if it fails, the test will continue?


#1

I have many points in my tests where I want to validate something, but the test should not fail and exit if that validation fails. Is it possible to tell Webtestit to continue the test if certain assertions don’t pass?


#2

Hello @ohrnsk!
Yes, of course. You could accomplish this in two ways:

  1. Use try/catch block with your assertions and catch the assertion exception, for example
        try {
            Assert.assertEquals(actual, expected);

        } catch (AssertionError e) {
            System.out.println("Assertion failed!");
        }
  1. Use the Soft Assert TestNG method:
// Instantiate SoftAssert 
SoftAssert softAssertion= new SoftAssert();

// Call the SoftAssert method
softAssertion.assertEquals(actual, expected);

softAssertion.assertAll();

In both cases, the test execution will continue even if the assertion would fail. In the first case, the exception is caught, and the message is printed to the log.
In the second case, SoftAssert will allow the execution of the rest of your test steps even after the failed assertion, and softAssertion.assertAll() method will collate the result of all the assertions and in case of any failure mark the whole test as failed.
Find out more about SoftAssert here.


closed #3