2
votes

I'm using TestNG soft assertions in my code like.

public class AssertionTest{
  private SoftAssert softAssert = new SoftAssert();

    @Test(enabled = true)
    public void testSoftAssertion(){
        softAssert.assertTrue(false);
        softAssert.assertTrue(true); 
        softAssert.assertEquals("India", "US");

        softAssert.assertAll();
    }
}

When test execution completes test fails(as expected) but result doesn't give details info, rather it gives info like following which doesn't help to understand which asserts failed.

FAILED: testSoftAssertion
java.lang.AssertionError: The following asserts failed:
null, null

I'm expecting output something which will help to understand the result(This type of output is generated when we use hard assertion i.e with Assert class).

FAILED: testSoftAssertion
java.lang.AssertionError: The following asserts failed:
expected [true] but found [false]
expected [India] but found [US]

Is this known defect/drawback with TestNG soft assertion or there is something, which I'm missing?

2

2 Answers

10
votes

you need to provide the failure message while asserting only then it will take up in the report. Consider below code snippet:

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

public class SoftAsert
{
    @Test
    public void test()
    {
        SoftAssert asert=new SoftAssert();
        asert.assertEquals(false, true,"failed");
        asert.assertEquals(0, 1,"brokedown");
        asert.assertAll();
    }
}

Output:

FAILED: test
java.lang.AssertionError: The following asserts failed:
failed, brokedown

That is how the softassertions are suppose to work. The intent is to display the error message in case of failure. Let me know if this helps.

-1
votes

Not sure if you resolved your formatting issues, but TestNG accepts formatting like \n for new lines within Strings (like in Reporting statements). Try taking a look at those kind of options.