Site icon TestingDocs.com

SoftAssert in TestNG Framework

Introduction

In this post, we will discuss SoftAssert in the TestNG framework. Sometimes, we might write more than one assert in a @Test method and want to make all the asserts run even if one or more fails. This can be achieved using SoftAssert in TestNG.

Using SoftAssert when an assertion fails, it doesn’t throw an exception but records the failure. Invoking the .assertAll() method, will cause an exception to be thrown if at least one assertion fails.

 

Hard Assert

package com.testingdocs.testng.tutorial;

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

public class HardAssertExample { 
	@Test 
	public void AssertTest() {
		Assert.assertEquals("testing","testingDocs.com", "Assert One"); 
		Assert.assertEquals("testingDoc.com","testingDocs.com",
 "Assert Two");
		Assert.assertFalse(true, "Assert Three"); 
		Assert.assertEquals(2,3, "Assert Four"); 
	} 
}

 

 

Hard Assert vs Soft Assert

Let’s assume we are testing a webpage with 4 issues that fail. If we use normal static Assert methods the test would fail with an assertion error on the first issue. SoftAssert allows all assertions to run, no matter if they pass or fail. Also, if there are failures, we may want to see all the issues in the test.

Before using soft assert we will need to instantiate it, like as shown below:

SoftAssert sa = new SoftAssert();

Sample Soft Assert Example

package com.testingdocs.selenium.demo;

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

public class SoftAssertExample
{
  SoftAssert sa = new SoftAssert();
  
  @Test
    public void softAssertTest() 
  {
        sa.assertEquals("testing","testingDocs.com", "Soft Assert One");
        sa.assertEquals("testingDoc.com","testingDocs.com", "Soft Assert Two");
        sa.assertFalse(true, "Soft Assert Three");
        sa.assertEquals(2,3, "Soft Assert Four");
        sa.assertAll();
    }

}

 

Run output

FAILED: softAssertTest
java.lang.AssertionError: The following asserts failed:
Soft Assert One expected [testingDocs.com] but found [testing],
Soft Assert Three expected [false] but found [true],
Soft Assert Four expected [3] but found [2]

Note that we can see that all the assertions have run and failed assertions are reported.

 

 

The difference between hard assert and soft assert is that soft assert allows all the asserts to be executed. Therefore, all of them will be run, even if they fail, but after all, are run, the results are displayed. These results include all the failures that were encountered during the test execution. As usual, if there is at least one failure, the test will be marked as failed as shown in the above example.

 

Conclusion

In the above test example, the sa.assertAll() method collects all the failures and decides whether to fail the test or not at the end. Also, if you forget to add the .assertAll(), the test will pass, and no failures will be reported.

 

 

TestNG Tutorials on this website can be found at:

https://www.testingdocs.com/testng-framework-tutorial/

For more details on the TestNG Framework, visit the official website of TestNG at:

https://testng.org

Exit mobile version