Assertion in TestNG

The assertion in TestNG means “Trust but Verify” Assertion is performed to compare the Actual result with expected result. In TestNG assertion is performed with the help of Assert Class.


Assert helps us to verify the conditions of the test and decide whether the test has failed or passed.  It is most popular and frequently used methods in TestNG while creating Selenium Scripts. Suppose,  In selenium, there will be the situation in the test where you need to check the presence of an element. All you need to do is to put an assert statement on to it to verify its existence.


TestNG Assertion Video Tutorial:

If you liked this video, then please subscribe to our YouTube Channel for more video tutorials.


The assertion is two types in TestNG.

1. Hard Assertion  – In hard assertion tests immediately fail and stop executing while assertion fails. You may want to use a hard Assert if you want to verify if you have logged in correctly and fail the test if you haven’t as there is no point in proceeding further if the test if the pre-condition itself fails.


2. Soft Assertion  – In soft assertion tests don’t stop running even if an assertion condition fails, but the test itself is marked as a failed. This is useful if you are doing multiple validations in a form and you may actually want to complete all the validations and fail the test once all validations are complete in case of failures.

But in soft assertion you need to add assertAll() method in the last statement to mark the test failure otherwise it will not mark the test fail in Test report. assertAll() method collates all the failures and decides whether to fail the test or not at the end. This should be the last statement in your test, the program will not execute after assertAll() if the assertion fails.


Methods of Assertion Class

  1. assertEquals(expected, actual) – This method is used to compare two objects in TestNG. If the assertion fails it will appear in the report.
  2. assertTrue() and assertFalse() – The assertTrue() and assertFalse() methods tests a single variable to see if its value is either true, or false. We also can use methods as arguments in the assertTrue() method.


Example of Assertion in TestNG

Let’s understand all these concepts with an easy example. In our example, We have test class named AssertionTest.

To verify the hard assertion and soft assertion first we need to create the object of Assertion and SoftAssert class available in TestNG.


We have 4 custom test methods in our example class, let’s discuss these methods and see the execution report.

  1. testForHardAssert() – We are using hard assertion here, so this will be failed in the report and given print statement shouldn’t be work because we are using hard assertion program will stop working immediately when assertion fail.
  2. testForSoftAssertWithNoFailure() – In the second method, We are using soft assertion but we are not using assertAll()  so this assertion will be failed but print statement should be work and error shouldn’t be reported because assertAll() is missing.
  3. testForSoftAssertionFailure() – In this method, we are using soft assertion with assertAll() . So execution should be continued and print statement should be work even after assertion fails and at last assertAll() should fail the test because assertion was failed.
  4. testForAssert() – This is last and in this, we are using Assert class neither hard assert nor soft assert. Assert class behaves like hard Assert and stop execution if the assertion fails.
    So in our example first print statement should be work but next shouldn’t be because the first assertion in this method is pass and next is failed.


Let’s execute the Test Class and verify our logic with a report.


Let’s see the Report. We are displaying TestNG console report here. You can also see these reports in your HTML reports.

TestNG Assestion


Have you noticed, All was executed as per our logic? So this the whole concept of assertion in TestNG. There are may more methods of assertion in Assert class you can practice these yourself.


Next, we will learn the main use of assertion while we will use TestNG with selenium.


0 Comment

Leave a Reply