Hire QA – Specialized in QA Recruitment, Technical Interviews and Testing Solutions

Difference between soft assertion and hard assertion in TestNG?

Category: TestNG

Soft assertions (SoftAssert) allows us to have multiple assertions within a test method, even when an assertion fails the test method continues with the remaining test execution. The result of all the assertions can be collated at the end using softAssert.assertAll() method.

@Test
public void softAssertionTest(){
SoftAssert softAssert= new SoftAssert();
//Assertion failing
softAssert.fail();
System.out.println(“Failing”);
//Assertion passing
softAssert.assertEquals(1, 1);
System.out.println(“Passing”);
//Collates test results and marks them pass or fail
softAssert.assertAll();
}

Here, even though the first assertion fails still the test will continue with execution and print the message below the second assertion.
Hard assertions on the other hand are the usual assertions provided by TestNG. In case of hard assertion in case of any failure, the test execution stops, preventing the execution of any further steps within the test method.

Leave a Reply

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