How To Use Soft Assert In TestNG | TestNG Tutorial
Before knowing what is Soft Assert, first let’s see what is an Assert and what is the disadvantage in using Assert and why we are moving to Soft Assert.
Asserts are used to perform validations in the test scripts.
There are two types of Assert:
- Hard Assert
- Soft Assert
When an assert fails the test script stops execution unless handled in some form. We call general assert as Hard Assert
Hard Assert – Hard Assert throws an AssertException immediately when an assert statement fails and test suite continues with next @Test
The disadvantage of Hard Assert – It marks method as fail if assert condition gets failed and the remaining statements inside the method will be aborted.
To overcome this we need to use Soft Assert. Let’s see what is Soft Assert.
Soft Assert – Soft Assert collects errors during @Test
. Soft Assert does not throw an exception when an assert fails and would continue with the next step after the assert statement.
If there is any exception and you want to throw it then you need to use assertAll() method as a last statement in the @Test and test suite again continue with next @Test as it is.
We need to create an object to use Soft Assert which is not needed in Hard Assert.
Check below video to see “Soft Assert in TestNG”
Please be patient. The video will load in some time.
If you liked this video, then please subscribe to our YouTube Channel for more video tutorials.
Let’s see a practical example:
Here I took two methods namely softAssert() and hardAssert().
In the softAssert() method, I have used SoftAssert class and intentionally passing value false in the assertTrue() method to make it fail
In thehardAssert() method, I simply used Assert and intentionally passing parameter value false in the assertTrue() method to make it fail
package softwareTestingMaterial; import org.testng.Assert; import org.testng.annotations.Test; import org.testng.asserts.SoftAssert; public class SoftAssertion { @Test public void softAssert(){ SoftAssert softAssertion= new SoftAssert(); System.out.println("softAssert Method Was Started"); softAssertion.assertTrue(false); System.out.println("softAssert Method Was Executed"); } @Test public void hardAssert(){ System.out.println("hardAssert Method Was Started"); Assert.assertTrue(false); System.out.println("hardAssert Method Was Executed"); } }
Execute the above script and see the console output. You could see only one failure.
Console Output:
[TestNG] Running: C:\Users\Administrator\AppData\Local\Temp\testng-eclipse--2097831536\testng-customsuite.xml hardAssert Method Was Started softAssert Method Was Started softAssert Method Was Executed =============================================== Default suite Total tests run: 2, Failures: 1, Skips: 0 ===============================================
Below script is same as the first one but just added assertAll() method in the end of the first method (i.e., softAssert()).
Note: If you forget to call assertAll() at the end of your test, the test will pass even if any assert objects threw exceptions as shown in the above example. So don’t forget to add assertAll()
package softwareTestingMaterial; import org.testng.Assert; import org.testng.annotations.Test; import org.testng.asserts.SoftAssert; public class SoftAssertion { @Test public void softAssert(){ SoftAssert softAssertion= new SoftAssert(); System.out.println("softAssert Method Was Started"); softAssertion.assertTrue(false); System.out.println("softAssert Method Was Executed"); softAssertion.assertAll(); } @Test public void hardAssert(){ System.out.println("hardAssert Method Was Started"); Assert.assertTrue(false); System.out.println("hardAssert Method Was Executed"); } }
Execute the above script and see the console output. There are two failures here. Second failure is due to assertAll() method
Console Output:
[TestNG] Running: C:\Users\Administrator\AppData\Local\Temp\testng-eclipse--147736931\testng-customsuite.xml hardAssert Method Was Started softAssert Method Was Started softAssert Method Was Executed =============================================== Default suite Total tests run: 2, Failures: 2, Skips: 0 ===============================================
You could find the complete TestNG tutorial here.
If you are not regular reader of my blog then I highly recommend you to signup for the free email newsletter using the below link.
Is there a way to combine both Hard and Soft Assertions in one method?
Here’s an example:
@Test
public void testMethod() {
softAssert.assertEquals(“test”, “not test”, “This shoud fail”);
hardAssert.assertTrue(false, “This should fail, too”);
softAssert.assertAll();
}
When I run this code, only the hardAssert gets reported because it already throws an exception. If I call on softAssert.assertAll() before hardAssert.assertTrue(), it only reports the softAssert failures.
I would like for both failures in hard and soft assertions to be reported when the test completes.
Thanks in advance!
Hi Mark, What do you want to achieve. Could you please share your thoughts.
If asert.assertAll(); is there in one method can I access next method if test fails?
My Test is getting failed when there is no assert :
//Create an object for SoftAssert
public static SoftAssert sa= new SoftAssert();
@Test
public static void oracleTest()
{
if(5 > 3)
{
sa.assertTrue(true);
System.out.println(“hello”);
}
sa.notifyAll();
}
I am sorry,
Its my mistake in the code, Instead of “sa.notifyAll();” I need to write “sa.AssertAll();”
Thanks Raj for your write up on Soft Assertions
It’s fine Siva. Keep up your good work.
lets say we have 5 assert statement in soft assert and 2 and 4 assert are failing after Assert all, I want to know exactly which assert statement failed and get email for that currently I am using it in after method but unable to get details on exactly which step it has failed.
ITestResult doesn’t have that option is there any work around which you could suggest?
Thanks
@AfterMethod
public void closeBrowser(ITestResult result) throws EmailException
{
driver.close();
if(ITestResult.FAILURE==result.getStatus()) {
System.out.println(result.getTestName());
EmailPage.sendFailedEmail(ProperyReader.getProperty(“ukNonMemberEmailId”), ProperyReader.getProperty(“emailPassword”), ProperyReader.getProperty(“sendEmailTo”),result.getName());
}
}
Try result.getName() instead of result.getTestName()
Tried that didn’t work.IT doesn’t give me details as on which exact asserts did the test failed on.
I want email with #2 assert and #4 assert failed.
Hi,
can any one tell me the real time usage of BeforeTest and BeforeClass methods in Selenium script?when do we use these Methods .what kind of logics do we keep in these methods?