Soft Asserts and Hard Asserts in TestNG



TestNG is mainly used by the JAVA Developers to perform Unit Testing of the Code. This Framework is used mainly in Selenium as well to verify the outcome of the automation test Scenario. To verify the outcome the assert statements are available in the org.testng.Assert Class used such as

  • Assert.assertEquals
  • Assert.assertTrue
  • Assert.assertFalse
  • Assert.assertNotNull

.
They are different types of Assert types such as

  • Hard Assert
  • Soft Assert

Hard Assert:

In Hard Assert if the assertion is failing the execution stops.
Assert.assertTrue statement fails the test and stops the execution and makes the test case as failing.
Assert.assertFalse statement continues the test, if the element is not present in the Web Application.
Assert.assertFalse statement stops the execution of the test, if the element is present in the Web Application.

Soft Assert:

In Soft Assert if the assertion is failed the execution doesn’t stops. The execution continues to the next step. The org.testng.asserts.SoftAssert object collects all the errors. If you need to throw an exception then you need to use the method assertAll() as the last statement in the @Test. And the execution continues to the next @Test.
assertAll fails the test case if any of the assertions are failed.

Summary

It’s important to decide what kind of Assertion is required.
If you want your assert method to fail and stops the execution use the Hard Assert.
If you want your assert method to fail and continues with the execution of the second assert and collect all the results at the end of the test use Soft Assert.

Leave a Reply

Your email address will not be published. Required fields are marked *