Soft Assertions using TestNG

23 July, 2014
We write various automation tests to validate something based on which we mark the test as passed/failed. We use Assertions in the test for validation. Basically, there are two types of Assertions: Hard Assertion and Soft Assertion.

Hard Assertions (commonly called as Assertions)

Tests immediately fail and stop execution the moment a failure occurs in the assertion. You may want to use a hard assert if you want to verify if you have logged in correctly and fail the test if you haven’t. There is no point in proceeding further if the test faile in the pre-condition itself.

Soft Assertions

Tests don’t stop running even if an assertion condition fails, but the test itself is marked as a failed test to indicate the right result. This is useful if you are doing multiple validations in a form where you may want to complete all the validations and fail the test once all validations are complete in case of failures.

There are multiple ways of doing soft asserts. You can write your own custom logic using the regular assert and trap any exceptions and make the test proceed further. Or, you can use the TestNG library (for Java) which does all the magic for you.

Initialize the TestNG assertions…
import org.testng.asserts.Assertion;
import org.testng.asserts.SoftAssert;
public class MyTest {
  private Assertion hardAssert = new Assertion();
  private SoftAssert softAssert = new SoftAssert();

Here is a sample test for a Hard Assertion…
public void testForHardAssert() {

Here is a sample test for Soft Assertion which does not fail the test…
public void testForSoftAssertWithNoFailure() {

And here is a soft assertion that actually marks the test as a failure…
public void testForSoftAssertionFailure() {
  softAssert.assertEquals(1, 2);

If you look at the above test, the softAssert.assertAll() does the trick. This method collates all the failures and decides whether to fail the test or not at the end. So instead of writing a custom logic, the TestNG library itself offers the facility to perform Soft Assertions in your test.

No comments:

Post a Comment