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

3 comments:

  1. Good Explanation . Pretty easy to understand and crisp

    ReplyDelete
  2. Very Well explained Sir. Thank You so much :)

    ReplyDelete
  3. appriciate, got clear picture of both Assertions. keep doing.

    ReplyDelete

DevOps 01: What is DevOps and How it benefits organizations?

DevOps is a culture in an organization, where the development team and operations team help each other by sharing information, process and t...