Transcripted Summary

In this chapter, we will discuss the difference between hard asserts and soft asserts.


# Difference Between Hard Asserts and Soft Asserts

A hard assert stops execution after a fail and moves on to the next annotation. It does not matter if the next annotation is a @Test annotation or a configuration annotation.

We saw in the previous chapter that steps 3, 4, and 5 did not execute because they were inside the same annotation that failed and used hard assertion. Therefore, the steps after the first fail were skipped and execution picked up at the next test method.

A soft assert continues execution after a fail and moves on to the next statement line. It was designed to keep executing even when a verification step fails.

To use a soft assert, first we declare SoftAssert which is a class in TestNG.

SoftAssert softassert = new SoftAssert();

Next, we replace all of the Assert references with our object reference softassert. The assertion methods assertEquals, assertFalse, and assertTrue will stay the same.


@Test
public void testHomePageVerification() 
{
    softassert.assertEquals(true, false, "The Welcome Link Is Not Correct On The Home Page");
    System.out.println("3. Verify Welcome Link");

    softassert.assertFalse(false, "The Admin Tab Is Not Displayed On The Home Page");
    System.out.println("4. Verify Admin Tab");
    
    softassert.assertTrue(false, "The Dashboard Is Not Correct On The Home Page");
    System.out.println("5. Verify Dashboard");		
}

The SoftAssert class actually only has two methods: assertAll and doAssert. We need to use assertAll every time for soft assert to work. If assertAll is not used then our test will pass. I’m going to run without assertAll then run with assertAll to show you the difference.

Without assertAll, we see that testHomePageVerification passes even though we know that the conditions are not met for steps 3 and 5. SoftAssert kept executing although the verification step failed.

Now, let’s add assertAll to the end of the test method


softassert.assertAll();

When we run this we see that testHomePageVerification now fails and the assertions which failed have been printed to the console:


java.lang.AssertionError: The following asserts failed:
	The Welcome Link Is Not Correct On The Home Page expected [false] but found [true],
	The Dashboard Is Not Correct On The Home Page expected [true] but found [false]

Here’s a diagram that shows assertAll. On the left, we see a few assertion methods and assertAll on the right. The arrows are pointing to assertAll because they indicate an AssertionError gets stored into assertAll if there is a failure. That’s why we place the assertAll method at the end of our test method.


TestNG assertAll

# Hard Asserts vs Soft Asserts

Which one should we use for automation? It depends on our test. Sometimes it’s good to have a hard assertion and other times it’s not good to have a hard assertion. The same with soft assertions. Let’s consider these print statements.


import org.testng.annotations.Test;

public class HardAssert_SoftAssert
{
	@Test
	public void exampleAssertTest ()
	{
		System.out.println("1. Open Browser");
		
		System.out.println("2. Open Application");
		
		System.out.println("3. Sign Into The Application");
		
		System.out.println("4. Go To Home Page & Verify Home Page");
		
		System.out.println("5. Go To Search Page & Verify Search Page");
		
		System.out.println("6. Search For User");
		
		System.out.println("7. Sign Out Of The Application");		
	}
}

We know hard asserts stop if a verification step fails and will not execute the next statement but soft asserts will execute the next statement.

If we had a failure after opening the browser then we would not want to continue executing the next step. There is no reason. With that scenario, it’s best to use a hard assert.

The same with opening an application. There is no reason to keep executing our test script if it’s a failure opening the application. The next step which is to sign into the application would return a failure.

However, we would not use a hard assert after step 4. If there’s a failure on the home page, we still want to verify step 5.

To sum up assertions, we want to use hard asserts where it does not make sense to keep executing our test after a verification failure. We use soft asserts when we want to continuing executing our test even in the event of a failure.



Resources



© 2024 Applitools. All rights reserved. Terms and Conditions Privacy Policy GDPR