Thursday, August 4, 2022

TestNG Assertions

 The Testng assertions would be the primary source of highlighting whether the automated test case is passed or failed.

TestNG provides an Assert class that has multiple methods to raise asserts. To use TestNG assertions, it is important to import required package in your java class : org.testng.Assert

Syntax of Assertion in TestNG :

Below is the generic syntax of testng assertion:

Assert.methodName(actual, expected);
  • Assert : This is the class inbuilt in TestNG framework
  • methodName : This is the name of the Assert class method
  • actual : This is the first parameter of the assert method in which the value is passed that the user gets from application under test
  • expected : This is the second parameter of the assert method in which the user passes the expected value

Types of Assertions:

  1. Hard Assertion 
  2. Soft Assertions

    Hard Assertion:
    Whenever a hard assertion is applied and an assertion statement fails, the assertion in testng throws an exception immediately and terminates the further execution of the same test case and simply continues with the execution of the next test case in the test suite. As soon as the hard assertion condition fails, the test case is marked as failed
    EX: Assert.assertEquals(actualValue,expectedValue);

    Soft Assertions: To implement soft assertion in testng, we use a SoftAssert class and it’s method assertAll() to throw all the exceptions collected during the test case execution. The soft assert basically performs assertion and if a condition fails to meet, it doesn’t throw an exception immediately, instead it continues with the next statement of the same test case until the method assertAll() gets called to throw all the caught exceptions.
    EX: SoftAssert softassert;
    softassert = new SoftAssert();

    softassert.assertEquals(actualValue,expectedValue);

    softassert.assertAll();

    When to use Hard and Soft Assertion

            Hard Assertions

Soft Assertions

Use case: Terminates the test case execution with exception as soon as the assertion condition doesn’t meet.

Use case: Validates all the assertion conditions, collects exceptions in case the assertion condition doesn’t meet and throws all exceptions when assertAll() method is called.

When to use: The scenario in which hard assertion is used best would be the login test scenario where if the login test fails, the test case execution must be terminated with an exception as there is no point of moving further without logging into the system.

When to use: Soft Assertion is best used in cases where the test statements of a test case are not dependent on each other. For example, if you are validating a form in which there are multiple fields to be validated, hence it is recommended to soft assert all the fields and then call assertAll() to throw all exceptions at the end of the test case.

TestNG Assert Methods

  1. Assert.assertEqual(String actual, String expected): This assertion method accepts two parameters i.e. the actual value and expected value to validate if the actual string is equal to the expected string or not. The assertion exception is thrown if both the strings are not equal.
  1. Assert.assertEqual(String actual, String expected, String message): This assertion method is similar to the assertion method discussed above, the only difference is that this method can accept one more string parameter as a message. In case the assertion condition is not met, the assertion error is thrown along with a message passed here.
  1. Assert.assertEquals(boolean actual, boolean expected): This assertion method accepts two boolean values and validates if both are equal or not.
  1. Assert.assertTrue(condition): This assertion method is used to assert whether the condition passed in a parameter returns true or not. If the condition returns false, the assertion error is thrown.
  1. Assert.assertTrue(condition, message): This assertion method is similar to the assertion method discussed in previous one, the only difference is that this method can accept one more string parameter as a message. In case the assertion condition is passed as false, the assertion error is thrown along with a message passed here.
  1. Assert.assertFalse(condition): This assertion method is used to assert whether the condition passed in a parameter returns false or not. If the condition returns true, the assertion error is thrown.
  1. Assert.assertFalse(condition, message): This assertion method is similar to the assertion method discussed in the previous one, the only difference is that this method can accept one more string parameter as a message. In case the assertion condition is passed as true, the assertion error is thrown along with a message passed here.
  1. Assert.assertNull(condition): This assertion method is used to assert whether the condition passed in a parameter returns null or not. If the condition doesn’t return null, the assertion error is thrown.
  1. Assert.assertNotNull(condition): This assertion method is used to assert whether the condition passed in a parameter returns value except null or not. If the condition returns null, the assertion error is thrown.

 

No comments:

Post a Comment