TestNG SoftAssert (2024) | TechGeekNext

TestNG SoftAssert (2024)

In this tutorial, we will understand about SoftAssert in TestNG. Inspired by the JUnit and NUnit which works as a testing structure for the Java programming language.

TestNG main goal is to cover tests on a wider range with more significant and easy processes for functional, unit, integration end to end, etc.

Q: What are SoftAssert in TestNG?
Ans:

SoftAssert is also one type of Assertion. Even though one asserts validation fails in soft asserts, the other assertions keep on running due to this implementation of the test do not stop.

In TestNG soft assert are not included by default.

In simple words, SoftAssert means a test method to continue implementation even after the failure of the first assertion. When there are many multiple assertions or after the assertion statement want to implement some other line of codes in the test method then this type of requirement arises.

TestNG provides a SoftAssert class for dealing with all these types of cases.
Syntax:

SoftAssert softAssert = new SoftAssert()

Q: What are the uses SoftAssert in TestNG?
Ans:

The uses SoftAssert in TestNG are as follow:

  1. SoftAssert in TestNG is used when we want to see exception errors, want the test implementation to proceed without caring about the failure of specific validations.
  2. Another name of the SoftAssert is Verify.
  3. In TestNG, SoftAssert is not included by default so the package org.testng. Softassert is to be included.
  4. SoftAssert is used when multiple assertion tests are to be done.
  5. The main use of SoftAssert is when the assertion is failed, Exception is not thrown but all the statements are implemented and after that test verifies the result of all the assertions and the test case result passed or failed is given on the basis of the assertion result.
  6. The collection of errors is done in the SoftAssert.

Types of TestNG Assert

TestNg Assert have two types as given below:

Hard Assert

Hard Asserts are asserts that terminate the test execution if an assert statement fails, and thus do not validate further assert statements.
Example:

Assert.assertEquals("user1", "user2", "validate the user");

Soft Assert

In SoftAssert, if one assert validation fails, further assertions continue to execute, in other words, the test execution does not come to a halt. Soft Assert is opposite to Hard Assert.

Internally, the SoftAssert Class extends the Assertion Class. As a result, SoftAssert provides all Assertion Class methods. It's also mentioned in the comment that assertAll() will throw an exception if at least one test case fails.

SoftAssert

Assertion Methods

Assertion

TestNG SoftAssert Example

In the below, a snippet of code shows three assertions are used. To show the methods are carried on in spite of the failure of the first two assertions.

The last point is very important as SoftAssert.assertAll() will collect the result of all the assertions and the result Passed or Failed is given for the test case.

When an assert fails, SoftAssert does not throw an exception; instead, it logs the failure.

If at least one assertion fails, an exception will be thrown when softAssert.assertAll() is used.

package com.techgeeknext.testcases;

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

public class LoginTestCases {

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

		softAssert.assertTrue("Test".equals("test123"), "Failing 1");
		System.out.println("Failing 1");

		softAssert.assertEquals(1, 1, "Passing Second ");
		System.out.println("Passing 2");

		softAssert.fail("Failing third ");
		System.out.println("Failing 3");


		//softAssert.assertAll();
	}

}

Output:
We can see that without softAssert.assertAll(), no exception is thrown. Soft Assertion

Now uncomment the softAssert.assertAll() code and run the test and we can see exception will be thrown. Soft Assertion

Recommendation for Top Popular Post :