Assertions

A test case is said to be passed when it is able to validate what is expected to do it. A test case can contains multiple steps. To make sure we have accomplished what we are expecting, we perform assertion. Assertion is performed on different ways expecting different outcome.



In TestNG, an assertion is a statement that verifies whether a given condition or expression is true or false. Assertions are essential in automated testing to validate whether the expected results of a test match the actual results. If an assertion fails during the execution of a test case, it indicates that there is a problem with the application under test.


TestNG provides a built-in assertion mechanism through the Assert class, which contains various assertion methods for different types of validations. There are two types of assertion:

  • Hard Assertion
  • Soft Assertion


Hard Assertion


A hard assert is the default assertion mechanism in TestNG. When a hard assert statement fails, it immediately stops the execution of the current test method and moves on to the next test method. Any subsequent code in the current test method after a failed hard assertion will not be executed. Hard asserts are implemented using methods from the Assert class.


Some commonly used assertion methods in TestNG include:
  • assertEquals(expected value, actual value): Checks if the expected value is equal to the actual value.
  • assertNotEquals(expected, actual): Checks if the expected value is not equal to the actual value.
  • assertTrue(condition): Checks if the given condition is true.
  • assertFalse(condition): Checks if the given condition is false.
  • assertNull(object): Checks if the object is null.
  • assertNotNull(object): Checks if the object is not null.
Java
Copy
import org.testng.Assert;
import org.testng.annotations.Test;
public class HardAssertions {
    String str;
    @Test
    public void assertionMethod()
    {
        Assert.assertEquals((2 + 2), 4);
        Assert.assertNotEquals(5 , 4);

        Assert.assertTrue(2 > 0);
        Assert.assertFalse(2 < 0);
        
        Assert.assertNull(str);
        str = "test";
        Assert.assertNotNull(str, "Assertion failed");
    }
}

Soft Assertions

In TestNG, a Soft Assert is an assertion mechanism that allows you to continue running a test even after an assertion failure occurs. Unlike regular (hard) assertions, which stop the test execution immediately upon failure, soft assertions collect all assertion failures and report them at the end of the test. This can be particularly useful when you want to capture multiple validation errors in a single test case rather than stopping the test after the first failure.

Following are the steps to use Soft assertions in TestNG:

  1. Import the necessary TestNG and soft assertion classes.
  2. Create an instance of the SoftAssert class.
  3. Use soft assertions to check conditions within your test methods using methods provided by the SoftAssert class. Common soft assertion methods include assertEquals, assertTrue, assertFalse, etc.
  4. After you have applied all the necessary soft assertions, you need to call the assertAll() method on the SoftAssert object at the end of your test method. This method will check all the assertions and reports any if any failures, but it doesn't stop the test execution

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

public class SoftAssertExample {
    String str;
    @Test
    public void assertionMethod()
    {
        SoftAssert softAssert = new SoftAssert();
        softAssert.assertEquals((2 + 2), 4);
        softAssert.assertNotEquals(5 , 4);

        softAssert.assertTrue(2 > 0);
        softAssert.assertFalse(2 < 0);

        softAssert.assertNull(str);
        str = "test";
        softAssert.assertNotNull(str, "Assertion failed");
        softAssert.assertAll();
    }
}

When to Use Hard Assert vs. Soft Assert

  • Hard Assert: Use hard assertions when you want the test to stop immediately upon encountering a failure, and there is no need to continue with subsequent test steps.
  • Soft Assert: Use soft assertions when you want to collect multiple assertion failures in a single test run and report them collectively at the end of the test method. This is useful for scenarios where you want to validate multiple aspects of a test case before deciding to fail it and want to provide more detailed reporting of all the failures.