Your Corporate Life

Difference between soft assert and verify

In TestNG, assertions are used to verify whether the actual output matches the expected output, which helps in validating test conditions. TestNG provides two types of assertions: HardAssert and SoftAssert. Each has its own use case and behavior, depending on the level of strictness required for a test.


1. HardAssert in TestNG

HardAssert (often simply referred to as Assert) is the default assertion type in TestNG. When a HardAssert fails, it immediately halts the execution of the test method. This is useful when you want a test to stop at the point of failure without executing the remaining steps.

Example Usage of HardAssert

import org.testng.Assert;
import org.testng.annotations.Test;

public class HardAssertExample {

    @Test
    public void testHardAssert() {
        System.out.println("Step 1: Start Test");

        // Hard assertion, if this fails, the test stops here
        Assert.assertEquals("Hello", "Hello", "Step 2: Checking if Hello equals Hello");

        System.out.println("Step 3: This will execute if assertion passes");

        // This will fail and stop the test
        Assert.assertEquals(5, 10, "Step 4: This assertion will fail and stop further steps");

        System.out.println("Step 5: This will not execute if assertion fails");
    }
}

In this example:

  • If an assertion fails, the test method will stop execution at that point.
  • The Assert.assertEquals(5, 10) line will fail, causing TestNG to stop the test and skip any following steps (i.e., “Step 5”).

Use Cases for HardAssert

  • Critical Checks: When a failure in an assertion means the rest of the test cannot proceed.
  • Short Test Cases: For tests with fewer validation points where failing any one condition invalidates the test.
  • Immediate Failure Logging: When you want to immediately know where the test fails without running remaining steps.

2. SoftAssert in TestNG

SoftAssert is more flexible than HardAssert. It allows the test to continue executing even if one or more assertions fail. All assertions are evaluated, and the results of each are logged. At the end of the test, TestNG will then mark the test as failed if any of the assertions failed.

To use SoftAssert, you must explicitly call the assertAll() method at the end of the test method. This method checks all assertions executed in that method and determines the overall test result.

Example Usage of SoftAssert

import org.testng.annotations.Test;
import org.testng.asserts.SoftAssert;

public class SoftAssertExample {

    @Test
    public void testSoftAssert() {
        SoftAssert softAssert = new SoftAssert();

        System.out.println("Step 1: Start Test");

        // Soft assertion, will not stop test execution if fails
        softAssert.assertEquals("Hello", "Hello", "Step 2: Checking if Hello equals Hello");

        System.out.println("Step 3: This will execute even if previous assertion fails");

        // This will fail, but the test will continue
        softAssert.assertEquals(5, 10, "Step 4: This assertion fails, but test continues");

        System.out.println("Step 5: This will still execute");

        // Must call assertAll() at the end to evaluate all soft assertions
        softAssert.assertAll();
    }
}

In this example:

  • If softAssert.assertEquals(5, 10) fails, the test continues to execute the following steps.
  • The assertAll() method at the end will collect all assertion results. If any soft assertions failed, it will mark the test as failed.

Use Cases for SoftAssert

  • Comprehensive Validation: When multiple validation points are independent, and you want to check all conditions in a single test.
  • Detailed Error Logging: Soft assertions allow capturing all assertion failures within a test method, providing a detailed report at the end.
  • Non-Critical Checks: For conditions that don’t necessarily prevent further execution if they fail (e.g., UI tests where multiple elements need verification).

3. Key Differences Between HardAssert and SoftAssert

FeatureHardAssertSoftAssert
Execution FlowStops test execution immediately on failureContinues executing test steps after failure
Assertion ResultsMarks test as failed immediately on failureCollects all results, marks test at end if any assertion fails
Error LoggingLimited, as it halts executionDetailed report with all failed assertions
Common Use CasesCritical tests, initial checksUI checks, multiple independent validations

4. Important Points About Using SoftAssert

  • Always Call assertAll(): The assertAll() method must be called at the end of a test method. If omitted, the test will pass even if there were failed assertions.
  • SoftAssert Instance: Each test method requires a new instance of SoftAssert. Do not share the same instance across multiple test methods.
  • Selective Use: Avoid overusing SoftAssert in scenarios where failures should immediately halt the test for accurate test outcomes.

Our Services: Your Path to Successful Testing Career

Are you looking to enhance your testing skills and boost your career? At Your Corporate Life, we provide tailored services for professionals like you to excel in the testing field. Here’s what we offer:

  1. Job Update Service: Get daily job updates on WhatsApp for Manual Testing, Automation Testing, ETL Testing, Developer, and DevOps roles with experience ranging from fresher to 12 years.
    • Subscription Fee: ₹499/- for 1 Year
  2. Naukri Profile Optimization: Enhance your visibility to recruiters with our expert Naukri profile optimization.
    • Fee: ₹999/-
  3. LinkedIn Profile Optimization: Boost your professional presence on LinkedIn with a profile that stands out.
    • Fee: ₹999/-
  4. Mock Interviews: Get interview-ready with our mock interviews designed to simulate real scenarios.
    • Fee: ₹499/- per interview
  5. Placement Assistance Service: Comprehensive support in job search and placement.
    • Fee: ₹2999/-
  6. Resume and Cover Letter Writing: Tailor-made resumes and cover letters that get you noticed.
    • Fee: Varies as per experience

Contact us today for more details and to give your career the boost it deserves: Click here to connect on WhatsApp.


By mastering assertions and leveraging our services, you can enhance your software testing expertise and advance in your career. Happy testing!

Leave a Comment

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

Scroll to Top