All of us writes automated tests and do the assertions as
per our test cases. Each test case might have more than one or more assertions.
Assume you have automated one test case and having more than one assertion. We
all know that if first assertion in the test case fails then test case will be
failed and do not validate remaining assertions in the test case.
For example, you automated one test case which validates UI Verification
in the Webpage and DB Verification in the back end system...etc. Now you wanted
your test case to validate both UI and DB Verification even any one of the
verification is failed. It is not possible with hard assertion as test case
will fail immediately when assertion condition fails.
TestNG support the following assertions.
Hard Assertions:
Test immediately fail and stop executing the moment a
failure occurs in assertion. You may want to use hard assertion in case of Pre
Condition of test case fails and no point of executing the test case in
further.
Soft Assertion:
Tests don’t stop running even if assertion condition fails,
but the test itself marked as a failed test to indicate right result. This is
useful if you are doing multiple validations like UI Page Multiple elements Verifications,
DB Verification...etc and you wanted to assert DB even any one of the UI Assertion
fails and fail the test case once all the validations are complete in case of
failures other Pass the test case.
Example:
package
automation.tests;
import
org.testng.asserts.Assertion;
import
org.testng.asserts.SoftAssert;
public
class Sample {
private Assertion hardAssert = new
Assertion();
private SoftAssert softAssert = new
SoftAssert();
}
@Test
public void testForHardAssert() {
hardAssert.assertTrue(false);
}
@Test
public void testForSoftAssertWithNoFailure() {
softAssert.assertTrue(false);
}
@Test
public void testForSoftAssertionFailure() {
softAssert.assertTrue(false);
softAssert.assertEquals(1, 2);
softAssert.assertAll();
}
}
If you look at the test case (testForSoftAssertionFailure), the softAssert.assertAll() does the trick instead of writing your owned custom logic. This method collates all the failures and decides whether to fail the test or not. So instead of writing custom logic, the TestNG library itself offers the facility to perform soft assertions in your test.
No comments:
Post a Comment