Soft Assertions in TestNG

I’d like to talk about TestNG’s one of the greatest feature which is Soft Assertions in TestNG. When we want to validate a result we use Assertion class of the testing libraries. We mostly use the hard ones. But we never use this word. We always say “Assertions”.

Hard assertions break your test whenever validation fails. So you need to wait for someone from the dev team to fix the issue or you just comment out that line to see if there are other failures. But that’s silly, isn’t it?

So we have Soft Assertions. Soft Assertions don’t fail your tests whenever validation fails. It waits till you say so.

Let’s see how soft assertions work on an example.

Hard Assertion Example

@Test
public void hardAssertTest() {
     Assert.assertEquals(2,2,"Error on 1st validation");
     Assert.assertEquals(1,1,"Error on 2nd validation");
     Assert.assertEquals(1,2,"Error on 3rd validation");
     Assert.assertEquals(2,1,"Error on 4th validation");
}

When you run this test, you will have below output.

java.lang.AssertionError: Error on 3rd validation expected [2] but found [1]
Expected :2
Actual   :1

Test failed after the first fail. You have no idea about the rest of the validations.

Soft Assertion Example

@Test
public void softAssertTest() {
    SoftAssert soft = new SoftAssert();
    soft.assertEquals(2,2,"Error on 1st validation");
    soft.assertEquals(1,1,"Error on 2nd validation");
    soft.assertEquals(1,2,"Error on 3rd validation");
    soft.assertEquals(2,1,"Error on 4th validation");
    soft.assertAll();
}

When you run this test, you will have below output.

java.lang.AssertionError: The following asserts failed:
	Error on 3rd validation expected [2] but found [1],
	Error on 4th validation expected [1] but found [2]

How To Use Soft Assertions in TestNG?

In order to use Soft Assertions, you need to create a SoftAssertion object by using below command.

SoftAssert soft = new SoftAssert();

Then you just use the same validation method like assertEquals, assertTrue, assertFalse etc… There are all the same. At the end of the test you just need to tell TestNG to evaluate all the validation. For that reason, you add below line at the end of the test function.

soft.assertAll();

Where to Use It?

Actually, it depends on your tests. Not every test is suitable for soft assertions. I am using Soft Assertions in some Selenium tests where I do input validations. But mostly, I use hard assertions.

Think about a registration screen with 10-15 input. They all have different validation and they are not dependent. You want to check those input’s validations. It would be best to use soft assertion in that screen so even if one of the validation fails, test execution will continue and we can validate other fields. You need to be wise about it.

Happy testing!

Canberk

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.