Tal Angel
Tal Angel

Reputation: 1788

Continue Test on TestNG with failures

I have a Test method with 2 assertions. The first assertion code fails, and because of that the 2nd one is not running.

How can I make all the line to run, without using SoftAsserter? I wish to accumulate all the errors, and at the and of my method to throw them.

Here is an example of my code:

@Test
public void SimpleTest()
{
    asserter.assertEquals(true,false);
    asserter.assertEquals(5,5);
}

Upvotes: 2

Views: 629

Answers (2)

Benoit
Benoit

Reputation: 5394

Here is how you could rewrite your tests without SoftAssert:

@Test
public void simpleTestFailure() {
    check(
            () -> Assert.assertEquals(true, false),
            () -> Assert.assertEquals(5, 5));
}

@Test
public void simpleTestOK() {
    check(
            () -> Assert.assertEquals(5, 5));
}

private void check(Runnable... runnables) {
    boolean success = true;
    int index = 0;
    for (Runnable runnable : runnables) {
        try {
            index++;
            runnable.run();
            System.out.println(String.format("Assertion %s succeeded", index));
        } catch (AssertionError ae) {
            System.err.println(String.format("Assertion %s failed:\n%s", index, ae));
            ae.printStackTrace(System.err);
            success = false;
        }
    }
    Assert.assertTrue(success);
}

This solution can be further refined to better match your needs.

Upvotes: 1

Infern0
Infern0

Reputation: 2814

You can use

@Test(alwaysRun=true)

test execution will continue no matter previous test has failed.

Upvotes: 0

Related Questions