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

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

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

Soft Assertion Example

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

How To Use Soft Assertions in TestNG?

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

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.

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