How to continue test execution after assertion failed in TestNG?
Last Updated :
12 Sep, 2024
TestNG is a powerful testing framework for Java that allows developers to create robust and flexible test suites. Assertions in TestNg are used to verify that the expected results match the actual outcomes during test execution.
By default, when an assertion fails, TestNG stops the current test method execution, which can be problematic if you want to continue running additional checks within the same test method. This article will explore methods to continue test execution even after an assertion fails, ensuring comprehensive test coverage and more informative test reports.
There are several solutions to address such situations:
- Soft Assertions: You can use the
SoftAssert
class to allow the test execution to continue even when an assertion fails. All the failures are reported at the end using the assertAll()
method. - Verification: Instead of using assertions, you can implement custom verification methods. These methods act like assertions but handle failures without stopping the execution. Since TestNG doesn't natively support verification, you need to create custom methods using
try-catch
blocks to manage the assertions and continue execution. - Try-Catch Block: Another approach is to use
try-catch
blocks. This technique ensures that even if an assertion fails, the test continues execution. However, it's more of a fail-safe mechanism and not specifically designed to ensure seamless continuation after every assertion failure.
In this article, let's illustrate how to continue execution when assertion is failed in testNG using try-catch Blocks.
Using try-catchtry-catch Blocks
- Handle assertion failures and continue execution is by using try-catchexceptions blocks.
- This approach allows you to catch AssertionError exceptions and handle them gracefully with the rest of the test logic.
Example
Java
import org.testng.Assert;
import org.testng.annotations.Test;
public class ContinueTestAfterFailure {
@Test
public void testTryCatch() {
try {
// First assertion (will fail)
Assert.assertEquals(1, 2, "First assertion failed");
} catch (AssertionError e) {
// Handle the assertion failure
System.out.println("Caught an assertion failure: " + e.getMessage());
}
// This code will still execute
System.out.println("Continuing test execution...");
// Continue with additional checks or assertions
Assert.assertTrue(1 < 2, "Second assertion failed");
}
}
Explanation
- Imports and Class Declaration:
import org.testng.Assert;
: Imports the Assert
class from TestNG, used for assertions.import org.testng.annotations.Test;
: Imports the Test
annotation from TestNG to mark methods as test cases.public class ContinueTestAfterFailure
: Declares the public class that contains the test method.
- Test Method:
@Test
: Marks the testTryCatch()
method as a TestNG test method. TestNG will execute this as part of the test suite.
- Try-Catch Block:
- First Assertion:
Assert.assertEquals(1, 2, "First assertion failed")
compares two values. Since 1 is not equal to 2, this assertion fails and throws an AssertionError
. - Handling the Failure: The
catch
block catches the AssertionError
and prints a message, allowing the test execution to continue. - Continuation: After catching the exception, the program continues to the next line, printing
"Continuing test execution..."
.
- Second Assertion:
Assert.assertTrue(1 < 2, "Second assertion failed")
: This assertion passes because 1 is indeed less than 2.
testng,xml
XML
<?xml version = "1.0" encoding = "UTF-8"?>
<suite name="Suite">
<test name="Test Suite">
<classes>
<class name="testNG.ExampleTest">
</class>
</classes>
</test>
</suite>
Output
OutputConclusion
Handling assertion failures gracefully can be crucial for comprehensive testing and debugging. By using SoftAssert, you can collect and report multiple assertion failures in a single test method while continuing execution. Alternatively, employing try - catch blocks provides another layer of control over assertion failures and allows the test method to method to proceed despite errors. Both methods enhance your ability to perform thorough testing and ensure that critical checks are not skipped due to a single failure.
Similar Reads
How to Execute Failed Test Cases in TestNG TestNG is a testing framework that simplifies many testing needs. It stands for Test Next Generation, an open-source test automation framework inspired by JUnit and NUnit. Think of TestNG as an updated version of the other two concepts. It provides additional features not previously available, such
7 min read
How to Stop Suite Execution after First Failure in TestNG? TestNG is the popular Java testing framework that offers annotations, configurations, and features in regards to test execution management. While working with TestNG there are a few cases when you might need to stop the execution of your test suite on the first failed test not to run some additional
3 min read
How to disable a TestNG test based on a condition? TestNG stands for the Test Next Generation, it is an open-source and automated framework that is used for testing, it is used for the testing of codes in Java programming language, Cedric Beust.TestNG includes lots of test categories such as integration, functional and unit testing, etc. and it allo
3 min read
How to get the test group name in TestNG before and after execution? In TestNG, test methods can be organized into groups, enabling you to execute specific tests together. Getting the name of a group before and after a test method will run helps you with logging, reporting, or changing how tests work based on their group.To achieve this, you can use the ITestContext
3 min read
How to Control Test Execution Order in TestNG? Controlling the order in which tests run is crucial, especially in larger software projects. By controlling the test execution order, you ensure that tests run logically. This helps in identifying real issues in the code, rather than false negatives caused by running tests out of sequence. It also m
3 min read