Understanding TestNG Assertions
Introduction
Assertions in TestNG are a way to verify whether the expected result and the actual result match 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 whether 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 assertions can significantly improve the clarity of 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
1.Hard Assert
Hard Asserts stop the test execution when an assert statement fails, and subsequent assert statements are not validated. They 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;
}
}
2.Soft Assert
Soft Assert 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 |
Behaviour 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 the realm of software testing, assertions are your trusty companions, aiding in the validation of expected outcomes. TestNG provides a robust set of assertion methods that cater to diverse testing scenarios. By incorporating these assertions into your test suites and adhering to best practices, you can enhance the reliability and effectiveness of your testing efforts. Embrace TestNG assertions, and empower your tests to unveil the true potential of your code. Happy testing! ๐งช๐
Subscribe to my newsletter
Read articles from Rinaldo Badigar directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
Rinaldo Badigar
Rinaldo Badigar
Software Engineer with 3+ years of experience in Automation, with a strong foundation in Manual, API, and Performance testing, ensuring high-quality software delivery for Ecommerce and Banking applications.