Mastering Assertions in TestNG
Introduction
Assertions in TestNG serve as powerful tools for validating expected outcomes in automated test scripts. By incorporating assertions, developers and testers can ensure that the application behaves as intended during various scenarios. TestNG provides a robust framework for implementing assertions, offering a wide range of methods to verify conditions and streamline the testing process.
Assertions in TestNG
Assertions in TestNG are a way to verify that the expected result and the actual result matched or not. An example of assertion can be logging into the website, checking the title of the webpage, verifying the functionality of an input box that takes only integers, etc.
We should remember that an assertion in TestNG is successful only if there are no exceptions thrown during the test case execution.
Syntax for TestNG Assertions
import org.testng.Assert;
public class AssertionExample {
public void testStringEquality() {
String actual = "Hello, TestNG";
String expected = "Hello, TestNG";
// Assertion using TestNG
Assert.assertEquals(actual, expected, "String equality check failed");
}
}
In this example, the assertEquals
method is used to verify if the actual string matches the expected string. If not, an error message ("String equality check failed") will be displayed.
How to Use Assertions in TestNG
import org.testng.Assert;
public class AssertionExample {
public void testNumericInequality() {
int actual = 10;
int expected = 5;
// Assertion using TestNG
Assert.assertNotEquals(actual, expected, "Numeric inequality check failed");
}
}
In this example, assertNotEquals
is used to ensure that the actual numeric value is not equal to the expected value. If the assertion fails, the specified error message ("Numeric inequality check failed") will be displayed.
Using Messages as a Parameter in TestNG Asserts
Adding a custom message to your assertions can significantly improve the clarity of your test reports. Let's enhance our previous example:
import org.testng.Assert;
public class AssertionExample {
public void testArrayEquality() {
int[] actualArray = {1, 2, 3};
int[] expectedArray = {1, 2, 3};
// Assertion using TestNG with a custom message
Assert.assertEquals(actualArray, expectedArray, "Array equality check failed");
}
}
Types of Asserts in TestNG
There are two types of Assert
Hard Assert :
It stops the test execution when an assert statement fails, and the subsequent assert statements are therefore not validated. Hard asserts are the default type of asserts in TestNG
import org.testng.Assert; import org.testng.annotations.Test; public class HardAssertExample { @Test public void testLogin() { // Perform login boolean loginSuccessful = performLogin("username", "password"); // Hard Assertion Assert.assertTrue(loginSuccessful, "Login failed"); // Subsequent statements executed only if login is successful // Perform other actions after successful login } private boolean performLogin(String username, String password) { // Implementation of login logic // Return true if login is successful, false otherwise return true; } }
Soft Assert :
It is the opposite of hard asserts where the subsequent assertions keep on running even though one assert validation fails, i.e., the test execution does not stop. To use this, you need to include the package org.testng.asserts.Softassert.
import org.testng.asserts.SoftAssert; import org.testng.annotations.Test; public class SoftAssertExample { @Test public void testProductDetails() { SoftAssert softAssert = new SoftAssert(); // Check product details softAssert.assertEquals(getProductName(), "Laptop", "Incorrect product name"); softAssert.assertEquals(getProductPrice(), 1200, "Incorrect product price"); softAssert.assertTrue(isProductInStock(), "Product out of stock"); // Mark test as failed if any soft assertion fails softAssert.assertAll(); } private String getProductName() { // Implementation to retrieve product name return "Laptop"; } private int getProductPrice() { // Implementation to retrieve product price return 1200; } private boolean isProductInStock() { // Implementation to check if the product is in stock return true; } }
Difference: Hard Asserts vs Soft Asserts
Feature | Hard Asserts | Soft Asserts |
Behavior on Failure | Stops test execution | Continues test execution |
Reporting Multiple Failures | Reports only the first failure | Reports all failures at once |
Continuation after Failure | Does not continue after failure | Continues after each assertion, even if previous ones fail |
Syntax | Assert.assertEquals(actual, expected, message) | SoftAssert softAssert = new SoftAssert();softAssert.assertEquals(actual, expected, message) |
Usage | Ideal for critical validations where further test steps are dependent on the success of the current one. | Suitable when you want to collect multiple assertions within a test without stopping at the first failure. Useful for scenarios where you want to capture all possible issues before terminating the test. |
Commonly Used TestNG Assert Methods
//assertEquals: Verifies that two values are equal.
Assert.assertEquals(actual, expected, "Assertion Failure Message");
//assertNotEquals: Verifies that two values are not equal.
Assert.assertNotEquals(actual, expected, "Assertion Failure Message");
//assertTrue: Verifies that the provided condition is true.
Assert.assertTrue(condition, "Assertion Failure Message");
//assertFalse: Verifies that the provided condition is false.
Assert.assertFalse(condition, "Assertion Failure Message");
//assertNull: Verifies that the provided object reference is null.
Assert.assertNull(object, "Assertion Failure Message");
//assertNotNull: Verifies that the provided object reference is not null.
Assert.assertNotNull(object, "Assertion Failure Message");
//assertThrows: Verifies that a specific exception is thrown during the execution of a test.
Assert.assertThrows(Exception.class, () -> {
Code that should throw the specified exception
},"Assertion Failure Message");
Conclusion
In conclusion, leveraging TestNG assertions enhances the reliability and effectiveness of test suites, enabling prompt identification of discrepancies in application behavior. By systematically incorporating assertions into test scripts, developers can achieve more accurate and dependable testing results, ultimately contributing to the overall quality and stability of the software under examination.
Subscribe to my newsletter
Read articles from Pankaj Suryavanshi directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by