TestNG Assertions – Types Of Asserts in TestNG

TestNG Assertions – Types Of Asserts in TestNG: When we automate any web application using Selenium webdriver, we have to add validation in our automation test script to verify that our test is passed or failed.

But Selenium WebDriver does not have any assertions. For using assertions, we have to use the TestNG Assertions with a single line of code, and we can verify that our test case passes or fails.

One thing that comes to your mind is that we are using TestNg assertions where we have log4j and System.out.println(), by which we learned that a test case is passed or failed.

If we use the log4j and System.out.println(), those print the result in the console but not in the report, but the result will not be reflected in the report. So, the report will be more helpful to the end-user or customer because he can get a clear idea from that.

In this post, we will discuss how to use the TestNG assertions and the various methods present in TestNG.

Types Of TestNG Assertions In Selenium

In Selenium, two types of assertions are categorized based on their behavior after a condition is passed or failed. The two assertions:

Hard Assertion
Soft Assertion

Hard Assertion

The Default build mechanism of assert is Hard assertion, and it’s stored in org.testng.assert package. We use this type of assertion when our requirement for the test execution should stop immediately if the assertions fail and will throw an assertion error.

The test case is also marked as failed when a hard condition fails.

Example:

package com.softwaretestingo.testng.assertion;
import org.testng.Assert;
import org.testng.annotations.Test;
public class HardAssertion 
{
	String className = "HardAssertion";

	@Test
	public void test_UsingHardAssertion() 
	{
		Assert.assertTrue(true == true);
		Assert.assertEquals("HardAssert", "HardAssertion");
		Assert.assertEquals(className, "HardAssertion");
		System.out.println("Successfully passed!");
	}
}

Output:

FAILED: com.softwaretestingo.testng.assertion.HardAssertion.test_UsingHardAssertion
java.lang.AssertionError: expected [HardAssertion] but found [HardAssert]

As you see in the output, when both actual and expected values are not matched on the below statement, we get the assertion error, and the test case is also marked as failed.

Assert.assertEquals("HardAssert", "HardAssertion");

Soft Assertion

This assertion is stored in org.testng.asserts.Softassert, and we need to use this type of assertion when we want to continue our execution even after the assertion got failed.

Soft assertions will not throw an error when the assertions fail, but it continue the execution to the next step.

The Soft Assertion is your automation script for use. First, you have to create an instance of the SoftAssert class, and by using that instance, you can call the various methods of the soft assertion.

package com.techbeamers.softassertion;
import org.testng.annotations.Test;
import org.testng.asserts.SoftAssert;
public class SoftAssertion 
{
   SoftAssert softAssert = new SoftAssert();
   String className = "SoftAssertion";

   @Test
   public void test_UsingSoftAssertion() 
   {
      softAssert.assertTrue(true == true);
      softAssert.assertEquals("SoftAssert", "SoftAssertion");
      softAssert.assertEquals(className, "SoftAssertion");
      System.out.println("Last statement gets executed!");
      softAssert.assertAll();
   }
}

You can see in the output that some of the assertions are still failing, but our execution is not stopped; it completes the execution.

Last statement gets executed!
FAILED: com.softwaretestingo.testng.assertion.SoftAssertion.test_UsingSoftAssertion
java.lang.AssertionError: The following asserts failed:
	expected [SoftAssertion] but found [SoftAssert]

The issues in using the Soft assertion

If there are multiple test methods and for all of those methods, if you use the same SoftAssert instance, then if one test fails, it makes other tests also fail.

It happens due to using the same assert object, which evaluates all occurrences of assert methods despite being in different cases.

package com.softwaretestingo.testng.assertion;
import org.testng.annotations.Test;
import org.testng.asserts.SoftAssert;
public class SoftAssertIssue 
{
	SoftAssert softAssert = new SoftAssert();
	String className = "SoftAssertion";

	@Test
	public void test1_UsingSoftAssertion() 
	{
		softAssert.assertTrue(true == true);
		softAssert.assertEquals("SoftAssert", "SoftAssertion");
		softAssert.assertEquals(className, "SoftAssertion");
		softAssert.assertAll();
	}

	@Test
	public void test2_UsingSoftAssertion() 
	{
		softAssert.assertTrue(true == true);
		softAssert.assertEquals("SoftAssertion", "SoftAssertion");
		softAssert.assertEquals(className, "SoftAssertion");
		softAssert.assertAll();
	}
}

Output:

FAILED: com.softwaretestingo.testng.assertion.SoftAssertIssue.test1_UsingSoftAssertion
java.lang.AssertionError: The following asserts failed:
	expected [SoftAssertion] but found [SoftAssert]
....
FAILED: com.softwaretestingo.testng.assertion.SoftAssertIssue.test2_UsingSoftAssertion
java.lang.AssertionError: The following asserts failed:
	expected [SoftAssertion] but found [SoftAssert]
....
===============================================
    Default test
    Tests run: 2, Failures: 2, Skips: 0
==============================================

If you see the output, you can find that test1 failed because the actual and expected values are not matching, but in the test2 method, there are no issues, but that also got marked as failed because we are using the same instance for all the test methods.

The right way to use the Soft assertion

In the above example, we have seen what the issues we will use SoftAssert properly to validate all the test methods properly. The simple solution is to create a separate SoftAssert for each test case or method.

package com.softwaretestingo.testng.assertion;
import org.testng.annotations.Test;
import org.testng.asserts.SoftAssert;
public class SoftAssertSolution 
{
	SoftAssert softAssert1 = new SoftAssert();
	SoftAssert softAssert2 = new SoftAssert();
	String className = "SoftAssertion";

	@Test
	public void test1_UsingSoftAssertion() 
	{
		softAssert1.assertTrue(true == true);
		softAssert1.assertEquals("SoftAssert", "SoftAssertion");
		softAssert1.assertEquals(className, "SoftAssertion");
		softAssert1.assertAll();
	}

	@Test
	public void test2_UsingSoftAssertion() 
	{
		softAssert2.assertTrue(true == true);
		softAssert2.assertEquals("SoftAssertion", "SoftAssertion");
		softAssert2.assertEquals(className, "SoftAssertion");
		softAssert2.assertAll();
	}
}

Output:

PASSED: com.softwaretestingo.testng.assertion.SoftAssertSolution.test2_UsingSoftAssertion
FAILED: com.softwaretestingo.testng.assertion.SoftAssertSolution.test1_UsingSoftAssertion
java.lang.AssertionError: The following asserts failed:
	expected [SoftAssertion] but found [SoftAssert]
....
===============================================
    Default test
    Tests run: 2, Failures: 1, Skips: 0
===============================================


===============================================
Default suite
Total tests run: 2, Passes: 1, Failures: 1, Skips: 0
===============================================

Different Assertion Methods

For Validation Purposes, TestNG provides many assertion methods, out of which we have mentioned a few methods that are used commonly:

assertEqual(String actual, String expected):– It takes two string arguments and checks whether both are equal; if not, it will fail the test.

assertEqual(String actual, String expected, String message):– It takes three string arguments and checks whether both are equal. If not, it will fail the test and throw the message we provide.

assertEquals(boolean actual, boolean expected):- It takes two Boolean arguments and checks whether both are equal; if not, it will fail the test.

assertEquals(java.util.Collection actual, java.util.Collection expected, java.lang.String message):- Takes two collection objects and verifies both collections contain the same elements and with the same order. If not, it will fail the test with the given message.

Assert.assertTrue(condition):– It takes one boolean argument and checks that a condition is true. If it isn’t, an AssertionError is thrown.

Assert.assertTrue(condition, message): It takes one boolean argument and String message. It Asserts that a condition is true. If it isn’t, an AssertionError is thrown with the given message.

Assert.assertFalse(condition): It takes one boolean argument and checks that a condition is false. If it isn’t, an AssertionError is thrown.

Assert.assertFalse(condition, message): It takes one boolean argument and String message. It Asserts that a condition is false. If it isn’t, an AssertionError is thrown with the given message.

Avatar for Softwaretestingo Editorial Board

I love open-source technologies and am very passionate about software development. I like to share my knowledge with others, especially on technology that's why I have given all the examples as simple as possible to understand for beginners. All the code posted on my blog is developed, compiled, and tested in my development environment. If you find any mistakes or bugs, Please drop an email to softwaretestingo.com@gmail.com, or You can join me on Linkedin.

Leave a Comment