top of page

Welcome
to NumpyNinja Blogs

NumpyNinja: Blogs. Demystifying Tech,

One Blog at a Time.
Millions of views. 

Assertions

May 22, 2025
4 min read

Updated: May 23, 2025

Assertions or Asserts are the checkpoints we include in our script to validate our tests in automation with selenium. They ensure that the application is working as it should or not. To put it simply, we use them as key checkpoints dependinerrerg on our needs. They allows us to know if the conditions for passing the test are met. Without assertions, you cannot find out if a test case has returned the right result.


In this tutorial, we are going to talk about Assertions, the different kinds of Assertions and Assertion methods through TestNG.



Types of Assertions

In Selenium, there are two types of assertions,

  • Hard Assertions

  • Soft Assertions (Verify Method)


Let’s take a closer look at each one of these.


Hard Assertions

Hard Assertions are ones in which test execution is aborted if the test does not meet the assertion condition. The test case is marked as failed. In case of an assertion error, it will throw the “java.lang.AssertionError” exception.

 

  • Key Characteristics:

    • Behavior: If a hard assertion fails, the test execution will halt, and the test will be marked as failed. 

    • Purpose: Hard assertions are used when it's crucial for a test to stop at the first failure, as a failure at that point could indicate a critical issue in the application. 

    • Example: Assert.assertEquals(actualValue, expectedValue) in TestNG. 


  • Assertion Types with examples:

    • assertEquals()

      This assertion method that takes a minimum of 2 arguments and compares actual results with expected results. If both match, the assertion is passed, and the test case is marked as passed. It can compare Strings, Integers, Doubles, and many more variables

Syntax:

Assert.assertEquals(actual, expected);
  • assertNotEquals() 

    This assertion method does the opposite of the assertEquals() method. In this case, the method compares the actual and expected result. But if the assertion condition is met if the two are not identical. The test case is marked as passed if actual and expected results are not the same.

Syntax: 

Assert.assertNotEquals(actual, expected, message);

  • assertTrue()

    This assertion method verifies the Boolean value returned by the condition. If the Boolean value is true, then the assertion passes the test case.

Syntax: 

Assert.assertTrue(condition);
  • assertFalse()

    This assertion method works the opposite of assertTrue(). The Assertion verifies the Boolean value returned by the condition. If the Boolean value is false, the assertion passes the test case.

    Syntax: 

Assert.assertFalse(condition);
  • assertNull()

    This assertion method verifies if the expected output is null. If not, the value returned is false.

    Syntax: 

Assert.assertNull(object);

  • assertNotNull()

    This assertion method works opposite to the assertNull() method. The assertion condition is met when the method validates the expected output to be not null.

    Syntax: 

Assert.assertNotNull(object);

Soft Assertions

Soft asserts are the asserts which continue the execution even after the Assert condition fails. They are mainly used to verify certain test conditions in the code. It is used in a case when the passing of one test condition is not necessary to execute the upcoming tests.

 

  • Key Characteristics:

    • Behavior: Soft assertions continue test execution even if the assertion fails. 

    • Purpose: Soft assertions are used when it's important to gather all possible failures within a test, even if some steps have issues. 

    • Example: SoftAssert.assertEquals(actualValue, expectedValue) in TestNG. 


    Note: Soft assertions typically collect all failures and report them at the end of the test.


  • How to Use Soft Asserts in TestNG?

Soft Asserts are not included by default in TestNG, hence in order to use them, we have to import

package from TestNG

  • Step 1: Create Instance for Soft Assert:

softAssert softAssert = new SoftAssert();

  • Step 2: After creating the instance for the soft assert, import the package

import org.testng.asserts.SoftAssert;

  • Step 3: In the end, we have to call the assertAll() method that is used to throw the exceptions and results at the end of the test.

softAssert.assertAll();

 

  • Assertion Types with examples:

Soft Assert also supports all the methods used by Hard Asserts such as,

  • AssertEquals

  • AssertTrue

  • AssertFalse

  • AssertNotEquals


Why Assertions Are Important

  • Check How the Application Functions:

    - Assertions ensure that the actual output matches expected results at key checkpoints.

  • Improve Test Efficiency:

    - With hard assertions, tests stop at the first step when needed conditions aren’t found.

  • Comprehensive Feedback:

    - If you are testing the UI and want to review multiple checks together, soft assertions come in handy since you can view them all in one place.

  • Fail-Fast Principle means to spot failures early and correct them (Hard Assertions):

    - Interrupts testing the moment a crucial function has issues, so other steps are not processed.

  • Reporting in detail (Soft Assertions):

    - Useful when you have to do full-page verifications and log many issues at the same time.


Conclusion

  • Designing and running dependable tests with Selenium WebDriver depends greatly on Hard and Soft Assertions.

  • They help confirm that the application acts as intended, mainly during important steps of test execution with Selenium WebDriver.

  • They prevent teams from spending valuable time and resources on invalid test paths.


 
 

+1 (302) 200-8320

NumPy_Ninja_Logo (1).png

Numpy Ninja Inc. 8 The Grn Ste A Dover, DE 19901

© Copyright 2025 by Numpy Ninja Inc.

  • Twitter
  • LinkedIn
bottom of page