Random Boy
Random Boy

Reputation: 9

How to continue the test Execution after failing by assertion

response = res.when().post(a.getGetBankAPI()).then().extract()
    .response();

//Thread.sleep(1000);
int statuscode = response.getStatusCode();
System.out.println(statuscode);

try {
    Assert.assertEquals(statuscode, 200);
} catch (AssertionError e) {
    org.testng.Assert.fail("Actual value is not equal to Expected value for");
}

System.out.println(getJsonPath(response, "error.message[0].msg"));
assertEqual(getJsonPath(response, "error.message[0].msg"), "branch should be 3 to 50 character long", "Error for passing Empty Branch");

Here I the test case is failing after the step print ln status code, but it was not mentioning as failed but it was simply skipping and total test execution is aborted here, if u usr printstacktrace(), it was not skipping but it was also not noted as failure

Upvotes: -1

Views: 2423

Answers (2)

Alexey R.
Alexey R.

Reputation: 8686

What you need is actually soft asserts. TestNG supports such type of asserts so here is an example:

@Test
public void softAssertDemo{
  SoftAssert assertions = new SoftAssert(); // Here you'll be collecting your failures
  assertions.assertTrue(false, "Failure 1");  // Do your regular asserts in this way
  assertions.assertTrue(true, "Pass");
  assertions.assertTrue(false, "Failure 2");
  assertions.assertAll(); // in the end call this to fail your test if there were the faliures
}

Upvotes: 0

Zakaria Shahed
Zakaria Shahed

Reputation: 2707

     try
     {
         Assert.assertEquals(statuscode, 200);
     }
     catch(AssertionError e)
     {
         org.testng.Assert.fail("Actual value is not equal to Expected value for");
     }

The way you handled by using the try and catch block possibly caused the issue of the test being skipped instead of failed.

It's not good practice to use try catch on assertion. But if you really need this then you can use it like this

catch(AssertionError e)
{
    throw new AssertionError("Actual value is not equal to Expected value for", e);
}

If you are using TestNg then you can use assertion in this way. So you do not need to use any try catch to print the message

  Assert.assertEquals("Actual","expected"," Actual result is not same as expected");

Upvotes: 0

Related Questions