How to continue test execution after assertion failed in TestNG?


A TestNG class can have different tests like test1, test2, test3 etc. There could be some failure while running the test suite and user may get failures in between of @Test methods. Once a test method gets failed, he wants to continue the execution so all failures can be found out at the time. By default, if a failure occurs in a @Test method, TestNG gets exit from the same @Test method and continue the execution from next @Test method.

Here, the use case is to continue the execution of next line even if an assertion failed in same @Test method.

There are multiple ways to resolve such use cases:

  • Use the soft assertion by using SoftAssert class. It continues the execution even an assertion is failed and all failures can be captured at last using assertAll() function.

  • Use verification instead of Assertions. Basically, all asserts will be changed as verify. Please Note, verify is not supported by TestNG. User should write their own custom methods using try−catch block to handle assertions and continue the execution.

  • Use try catch block. It is moreover fail−safe technique and not exactly to execute next line of code even assertion failed.

In this article, let’s illustrate how to continue execution when assertion is failed in TestNG using SoftAssert.

Approach/Algorithm to solve this problem

  • Step 1: import org.testng.annotations.Test for TestNG.

  • Step 2: Write an annotation as @test in NewTest class

  • Step 3: Create a method for the @Test annotation as testCase1.

  • Step 4: Add multiple assertions using SoftAssert class as shown in program section.

  • Step 5: Repeat the steps for testCase2. Add assertion using assert.

  • Step 6: Now create the testNG.xml.

  • Step 7: Now, run the testNG.xml or directly testNG class in IDE or compile and run it using command line.

Example

The following code to create a TestNG class and displays the Listener functionality:

src/NewTest.java

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


public class NewTest {

    private SoftAssert softAssert = new SoftAssert();
    @Test(priority = 0)
    public void testCase1() {
        System.out.println("in test case 1 of NewTest");
        softAssert.assertTrue(false);
        softAssert.assertNotEquals(5,6);
        softAssert.assertEquals(1, 2);
        softAssert.assertAll();
    }
    @Test(priority = 1)
    public void testCase2() {

        System.out.println("in test case 2 of NewTest");
        Assert.assertTrue(true);
    }
}

testng.xml

This is a configuration file that is used to organize and run the TestNG test cases.

It is very handy when limited tests are needed to execute rather than full suite.

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "https://testng.org/testng-1.0.dtd">
<suite name="Parent_Suite">
    <test name="test">
        <classes>
            <class name="NewTest" />
        </classes>
    </test>
</suite>

Output

[INFO] Running TestSuite
in test case 1 of NewTestngClass
in test case 2 of NewTestngClass
[ERROR] Tests run: 2, Failures: 1, Errors: 0, Skipped: 0, Time elapsed: 0.832 s <<< FAILURE! - in TestSuite
[ERROR] NewTest.testCase1  Time elapsed: 0.016 s  <<< FAILURE!
java.lang.AssertionError: 
The following asserts failed:
	expected [true] but found [false],
	expected [2] but found [1]
	at NewTest.testCase1(newTest.java:15)
[INFO] 
[INFO] Results:
[INFO] 
[ERROR] Failures: 
[ERROR]   NewTest.testCase1:15 The following asserts failed:
	expected [true] but found [false],
	expected [2] but found [1]
[INFO] 
[ERROR] Tests run: 2, Failures: 1, Errors: 0, Skipped: 0

Updated on: 16-Aug-2023

811 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements