How to continue execution when Assertion is failed

大城市里の小女人 提交于 2019-11-26 08:27:50

问题


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.


回答1:


Selenium IDE uses verify to perform a soft assertion, meaning that the test will continue even if the check fails and either report the failures at the end of the test or on the event of a hard assertion.

With TestNG it is possible to have these soft assertions by using custom test listeners. I have documented how to do this on my blog: http://davehunt.co.uk/2009/10/08/using-soft-assertions-in-testng.html

Basically, you need to create your own verify* methods, in these you can catch assertion failures and add them to a map. Then in a custom afterInvocation listener you can set the test to failed if the map is not empty.




回答2:


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/




回答3:


Change your assertions to verifications:

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



回答4:


I am adding again one of the easiest ways to continue on assertion failure. This was asked here.

try{
        Assert.assertEquals(true, false);
        }catch(AssertionError e)
        {
            System.out.println("Assertion error. ");
        }

        System.out.println("Test Completed.");



回答5:


Once an assertion fails, execution should stop, that's the point of using them.

You can declare an assertion that tests both things, but then you're testing two things at once. Better to fix the cause of the first failure, then move on to the second assertion.



来源:https://stackoverflow.com/questions/5402412/how-to-continue-execution-when-assertion-is-failed

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!