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

Java Program 6: How to reverse a String in Java?

In this post we will discuss different methods to reverse a String in java programming. Method 1: Step 1: Read the input string using ...