Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to continue execution when Assertion is failed

I am using Selenium RC using Java with eclipse and TestNG framework. I have the following code snippet:

assertTrue(selenium.isTextPresent("Please enter Email ID"));
assertTrue(selenium.isTextPresent("Please enter Password"));

First assertion was failed and execution was stopped. But I want to continue the further snippet of code.

like image 602
Ripon Al Wasim Avatar asked Mar 23 '11 08:03

Ripon Al Wasim


People also ask

How do you continue the test even if the assert fails?

They don't throw an exception when an assert fails. The execution will continue with the next step after the assert statement. If you need/want to throw an exception (if such occurs) then you need to use assertAll() method as a last statement in the @Test and test suite again continue with next @Test as it is.

What happens if an assert statement fails?

If an assertion fails, the assert() macro arranges to print a diagnostic message describing the condition that should have been true but was not, and then it kills the program. In C, using assert() looks this: #include <assert. h> int myfunc(int a, double b) { assert(a <= 5 && b >= 17.1); … }

What happens if assert fails in selenium?

Difference between Assert and Verify in selenium In the case of assertions, if the assert condition is not met, test case execution will be aborted. The remaining tests are skipped, and the test case is marked as failed. These assertions are used as checkpoints for testing or validating business-critical transactions.


1 Answers

I suggest you to use soft assertions, which are provided in TestNg natively

package automation.tests;

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

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

@Test
public void testForSoftAssertionFailure() {
  softAssert.assertTrue(false);
  softAssert.assertEquals(1, 2);
  softAssert.assertAll();
}

Source: http://rameshbaskar.wordpress.com/2013/09/11/soft-assertions-using-testng/

like image 121
Stormy Avatar answered Oct 22 '22 22:10

Stormy