How to use Assertion in Automation Test Scripts?

As you already know that when we execute our test cases, we usually decide the status of execution pass or fail on the basis of comparison of expected and actual outcome.

The same we need to do in our automation test scripts. As without verify the results we cannot ensure our test script’s status.  To verify the results in automation, we use Assertion.

Now, the question arises in everybody’s mind is that what is Assertion and how to use it. Let’s start the discussion about it.

What is Assertion?

The assertion is a feature in TestNG which is used to fail the test scripts whenever the expected result is not matching with the actual result.

 

We can easily verify the test result pass or fail with the assertion. Assertion plays a major role in identifying the issues in the application. We can consider a test script successful only if it doesn’t throw an exception.

Types of Assertion

The assertion is of two types.

  1. Assert or Hard Assert
  2. Soft Assert

Assert or Hard Assert

Assert will fail the script when expected result is not matched with the actual result. We use assert for basic and the critical features.

Assert is the default mechanism in TestNg, it is present inside org.testng package. We use it when we want to stop the execution immediately when a script fails.

Hard Assert will throw an Assert Exception immediately when an assert statement fails.

Methods used in Assert or Hard Assert

All the methods present inside Assert class are static. The methods which we use commonly are mentioned below.

assertEquals(String actual,String expected):   

This method takes two arguments of String type and checks whether both are equal or not. It will fail the script if the arguments are not matching.

assertEquals(String actual, String expected, String message):

It will take three arguments of String type and checks whether both the results are equal or not. If the results are not equal then it will fail the script and throw the message which you provide.

assertEquals(boolean actual,boolean expected):

It will take two boolean arguments and verifies that both are matching or not. It will fail the script if the arguments are not matching.

assertNotEquals(String actual, String expected, String message):

The functioning of this method is completely opposite to assertEquals method. It will fail the script when both the argument matches. This method will throw the message which you provide.

assertTrue(condition):

This method takes one argument of boolean type and checks whether it is true or not. This method will fail the script and throw assertion error if it is not true.

assertTrue(condition, message):

It takes two arguments, one is the condition which is of boolean type and other is a message which is of String type. It will fail the script if the condition is not true and throw the error message which you provide.

assertFalse(condition):

It will take one argument of boolean type and checks whether the condition is false or not. If the condition is true it will throw an Assertion error.

assertFalse(condition, message): 

This method will take two arguments one is a boolean type and another is String type and verifies that the condition is false or not. If the condition is not false then it will throw the error message which is provided in the argument.

assertNull(object):

It will take an object as an argument and verifies if it is null or not. If it is notNull it will throw an Assertion error.

assertNotNull(object):

This method is just opposite to assertNotNull(object), it will verify that the object should not be null. If it is null then it will throw an Assertion error.

 Below is a sample code that how can you use these methods:

 @Test
 public void assertEquals(){
 Assert.assertEquals("Passed","Passed");
 System.out.println("This line is executed because assertEquals passed since both the strings are same");
 Assert.assertEquals("Passed","Failed");
 System.out.println("This line will not be executed because assertEquals fails both the strings are different."
 + "Also the test/method will be declared failed");
 }
 @Test
 public void assertNotEquals(){
 Assert.assertNotEquals("Passed","Since the expected and actual result do not match");
 System.out.println("This line is executed because assertNotEquals assertion pass for the given situation");
 }
 @Test
 public void assertTrue(){
 Assert.assertTrue(10>5);
 System.out.println("This line will execute as assertTrue will pass because the 10>5(which will return true)");
 }
 @Test
 public void assertFalse(){
 Assert.assertFalse(5>10);
 System.out.println("This line is executed because assertFalse assertion passes as the given condition will return false");
 }
 @Test
 public void assertNull(){
 Assert.assertNull(null);
 System.out.println("Since we set null in the condition, the assertion assertNull will pass");
 }
 @Test
 public void assertNotNull(){
 Assert.assertNotNull("It will pass ", "string don't returns a null value");
 System.out.println("This line will execute because assertNotNull have passed");
 }

Soft Assert

If you want to continue with the execution of remaining test steps even after the Assertion fails, in that case, you need to use Soft Assert. We use Soft Assert for minor features. In this class all the methods are non-static and if you want to fail the script you need to use assertAll() method.

Methods used in Soft Assert

In Soft Assert all the methods are same which are already defined in Hard Assert. There is only one difference in these methods which is, in Hard Assert all the methods are static and in Soft Assert all the methods are non-static. There is only one method which is different from Hard Assert. As all the methods are non-static that means you need to create an object of SoftAssert class to call these methods.

assertAll():

This method is used to fail the test script when the assert condition doesn’t match.

Below is the example that how can you use this method:

@Test

public void verifyResult(){

String expResult="Googl";

String actResult= driver.getTitle();

SoftAssert softassert=new SoftAssert();

 softassert.assertEquals(expResult, actResult, "Titles are not matching");

System.out.println("Result Verified");

softassert.assertAll();

}

Above mentioned scenario is when your test script fails, here assertAll() will immediately stop the execution of this test case.

Execute the above script and you got to know that how assertAll() will fail the script and stops the execution.

This is all about Assertions in TestNG, you have to use it in order to verify your test results. Without verifying the results you cannot ensure that you have successfully executed your test scripts. It will also help you to analyze the test results when your script fails.

Follow me!

Leave a Comment