API Testing:



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.

Must Read: Difference between Assert & Verify in Selenium

Asserts are used to perform validations in the test scripts.

There are two types of Assert:

  1. Hard Assert
  2. 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

Execute the above script and see the console output. You could see only one failure.

Console Output:

Soft Assert

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()

Execute the above script and see the console output. There are two failures here. Second failure is due to assertAll() method

Console Output:

Soft Assert

You could find the complete TestNG tutorial here.

TestNG Complete Tutorial

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.

Sharing is caring.

Share on facebook
Share on twitter
Share on linkedin

Like This Post?

We have a lot more where that came from?

We only send really good stuff occasionally, promise.

Rajkumar SM

11 thoughts on “How To Use Soft Assert In TestNG | TestNG Tutorial”

  1. Is there a way to combine both Hard and Soft Assertions in one method?

    Here’s an example:

    public void testMethod() {
    softAssert.assertEquals(“test”, “not test”, “This shoud fail”);
    hardAssert.assertTrue(false, “This should fail, too”);

    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!

  2. My Test is getting failed when there is no assert :

    //Create an object for SoftAssert
    public static SoftAssert sa= new SoftAssert();

    public static void oracleTest()
    if(5 > 3)

  3. 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

  4. 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?
    public void closeBrowser(ITestResult result) throws EmailException

    if(ITestResult.FAILURE==result.getStatus()) {
    EmailPage.sendFailedEmail(ProperyReader.getProperty(“ukNonMemberEmailId”), ProperyReader.getProperty(“emailPassword”), ProperyReader.getProperty(“sendEmailTo”),result.getName());

  5. 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?

Leave a Comment

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

Share on facebook
Share on google
Share on twitter
Share on linkedin
Share on whatsapp

Recent Posts:

Scroll to Top
API Testing eBook