Open In App

How to add custom messages to TestNG failure?

Last Updated : 13 Sep, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

In TestNG, you can add custom messages to test failures in TestNG to make clearer why a test didn't pass. You do this mainly by using the assertions where you give a special message showing up in the test report in case an assertion fails. Most of the time this will be achieved by using those methods on the Assert class that allow special messages.

Steps to Add Custom Messages to Test Failures

Following are the steps to add custom messages to test Failure in TestNG.

  • Create Test Class: Create a class that contains one or more test methods marked with @Test.
  • Use Assertions with Custom Messages: TestNG's Assert class has methods like assertEquals, assertTrue, assertFalse, and others, which can take as an extra custom message. This message will appear in case the assertion fails. A clear custom message can be helpful on occasions when something goes wrong.
  • Setup testng.xml: Create a testng.xml configuration file to specify which test classes and methods need to be included in the test suite; ensure that the XML points to your test classes correctly.
  • Test Run: On running test, as it fails the custom failure message would be helpful for traceability of the problem in TestNG report.

Example Test Class with Custom Failure Messages

CustomMessageTest.java

Java
package testNG;

import org.testng.Assert;
import org.testng.annotations.Test;

public class CustomMessageTest {

    @Test
    public void testMethod1() {
        int actualValue = 5;
        int expectedValue = 10;

        // Assertion with a custom failure message
        Assert.assertEquals(actualValue, expectedValue, "testMethod1 failed: Expected 10 but found 5.");
    }

    @Test
    public void testMethod2() {
        String actualText = "Hello";
        String expectedText = "Hello World";

        // Assertion with a custom failure message
        Assert.assertTrue(actualText.equals(expectedText), 
            "testMethod2 failed: Expected 'Hello World' but got 'Hello'.");
    }
}

Example testng.xml Configuration

The testng.xml file configures the suite and specifies the test classes to be executed.

testng.xml

XML
<?xml version="1.0" encoding="UTF-8"?>
<suite name="Suite" verbose="1">
    <test name="Test Suite">
        <classes>
            <!-- Include the test class with custom failure messages -->
            <class name="testNG.CustomMessageTest"/>
        </classes>
    </test>
</suite>

Explanation

  • Java Classes:
    • CustomMessageTest contains test methods that use assertions with custom messages. When an assertion fails, the provided message helps clarify the failure reason.
  • TestNG XML:
    • The testng.xml file defines the suite and test configurations.
    • The <class name="testNG.CustomMessageTest"/> entry specifies that the CustomMessageTest class should be included in the test run.

Output

Console

Output
output


Result of running suite (testMethod1)

output1
testMethod1

Result of running suite (testMethod2)

output2
testMethod2

Conclusion

By adding custom messages to your TestNG assertions and properly configuring your testng.xml file, you can enhance the readability and usefulness of your test reports. Custom messages provide immediate context on why a test failed, making it easier to identify and fix issues.


Next Article
Article Tags :

Similar Reads