断言失败时如何继续执行
我正在使用 Selenium RC,使用 Java 和 eclipse 和 TestNG 框架。我有以下代码片段:
assertTrue(selenium.isTextPresent("Please enter Email ID"));
assertTrue(selenium.isTextPresent("Please enter Password"));
第一个断言失败并且执行停止。但我想继续进一步的代码片段。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
我建议您使用 TestNg 中原生提供的软断言
来源:http:// /rameshbaskar.wordpress.com/2013/09/11/soft-assertions-using-testng/
I suggest you to use soft assertions, which are provided in TestNg natively
Source: http://rameshbaskar.wordpress.com/2013/09/11/soft-assertions-using-testng/
Selenium IDE 使用 verify 来执行软断言,这意味着即使检查失败,测试也会继续,并在测试结束时或在发生硬断言时报告失败。
使用 TestNG,可以通过使用自定义测试侦听器来获得这些软断言。我已经在我的博客上记录了如何执行此操作: http://davehunt.co.uk/2009/10/08/using-soft-assertions-in-testng.html
基本上,您需要创建自己的
verify*
方法,在这些方法中您可以捕获断言失败并将它们添加到映射中。然后,在自定义afterInspiration
侦听器中,如果映射不为空,您可以将测试设置为失败。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 customafterInvocation
listener you can set the test to failed if the map is not empty.我再次添加了继续断言失败的最简单方法之一。这是此处提出的问题。
I am adding again one of the easiest ways to continue on assertion failure. This was asked here.
将您的断言更改为验证:
Change your assertions to verifications:
一旦断言失败,执行就应该停止,这就是使用它们的目的。
您可以声明一个测试这两个事物的断言,但随后您会同时测试两个事物。最好解决第一次失败的原因,然后继续进行第二次断言。
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.