Sunday, July 7, 2019

Soft Assertion Vs Hard Assertion in TestNG

In test automation, when we want to validate the results or some conditions we use Assertion class. If the condition fails, subsequent steps will be aborted and test will be marked as failed. This type of assertion is called Hard Assertion.

The disadvantage of Hard Assertion is when we have more assertions in a test and if the assertion at the beginning fails all the subsequent assertions will be not executed/validated. And if we create one test case for each assertion that is not good practice.

Hard Assertion Example: In the below example, we have 3 assertions verifying employee name, employee id and employee email.


When we execute the code, the first assertion is failed and next two assertions are not executed.


Here comes Soft Assertions which helps to validate all the assertions even if one among them fails. Soft Assertion is a feature provided by TestNG. It is same like Hard Assertion but it doesn't abort the subsequent steps when a condition fails, it will continue executing the subsequent steps also.

Soft Assertion Example: Lets the take above example and use Soft Assertion.

When we execute the code, even though the first assertion fail it will continue to second and third assertions. And we used  assertAll() method of soft assertion  to know which assertions passed and which assertions failed.


Hope you understood the difference between hard and soft assertions. Please comment if any queries.

Thanks for reading. #HappyLearning #HappyTesting

No comments:

Post a Comment

How do you identify broken links in Selenium Automation?

When we are asked to identify the broken links in the application, what we do? Collecting all the links on the page to a list and looping t...