2

I know there is a difference between assert and verify in Junit; the assert results in a runtime exception, causing test method execution to abort. To overcome this problem, we can have verify which will make test case fail, but allowing test execution to proceed rather than getting aborted.

I doubt there is any such built in approach in testNG. I do understand that best practices say, there should not be multiple asserts in same test case.

But, in practical world, we do come across scenarios where we need to have multiple checkpoints in one test case and we don't want our test method to abort because of one check point failure (test execution should proceed despite failure).

How can we use a soft assert mechanism, similar to verify, which will not abort test method when there is a checkpoint failure.

Example:

@Test(dataProvider = "getMailUrl")
public void testMail(email,subject);
Assert.assertFalse(subject.isEmpty);//1st checkpoint
Assert.assertNotNull(email); //2nd checkpoint
Assert.assertEquals(email, "feedback@yoursite.com"); //3rd checkpoint

Now, I want test execution to proceed, even if the 1st checkpoint (validation of subject being not empty) fails. Definitely we will be not be using assert (hard assert). We will need concept of soft assert similar to verify (facilitated by Junit).

I understand, we have to build a customListener implementing TestNG interfaces or extending existing listeners such as TestListenerAdapter.
I am still struggling - how can we do this?

3

TestNG has a feature called soft assert. This kind of assert doesn't stop the test when assert is failing.

First initialize it:

import org.testng.asserts.Assertion;
import org.testng.asserts.SoftAssert;

public class MyTest {
  private SoftAssert softAssert = new SoftAssert();
}

Then use it in your test:

@Test(dataProvider = "getMailUrl")
public void testMail(email,subject) 
softAssert.assertFalse(subject.isEmpty);//1st checkpoint
softAssert.assertNotNull(email); //2nd checkpoint
softAssert.assertEquals(email, "feedback@yoursite.com"); //3rd checkpoint
  • yes, testNG supports softAssert. But, it has a drawback. After using softAssert for each of checkpoints, we have to use softAssert.assertAll() at the end. It will validate all checkpoints. If any of checkpoints fail, test method will be marked as fail. But, it wont mention the check point which failed. It becomes painful to identify which checkpoint causes test method to fail. It would be nice if softAssert could also mention the checkpoint(assert) which caused failure. – Debasish Pradhan Apr 24 '15 at 13:45
  • I think there may have been an update recently that cleaned up the softAssert. I had an issue with the output all being on one line. Also, like any other assert, you can add a message indicating what failed for the checkpoint. Finally, I also learned you should re-initialize the softAssert if your class has multiple tests and uses the same softAssert object, otherwise your softAsserts will stack. – Emmanuel F. Jul 13 '16 at 17:49
0

Instead of using asserts just use ifs. If its a fail add an error string to a temp array. At the end of the test assert that the temp array is empty, as the fail reason print out the strings in the array.

Not sure why you would continue the test after the first fail, since it failed someone needs to look into anyways. The fixer should run the full test after fixing and see what else is broken as soon as possible.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.