'断言'与“验证”相比硒
Selenium 执行的检查通常有两种形式:assertFoo 和 verifyFoo。据我所知,assertFoo 使整个测试用例失败,而 verifyFoo 只是记录该检查的失败并让测试用例继续进行。
因此,使用 verifyFoo,即使其中之一失败,我也可以获得多种条件的测试结果。另一方面,对我来说,一次失败的检查就足以知道我的编辑破坏了代码,无论如何我都必须纠正它们。
在哪些具体情况下,您更喜欢这两种检查方式中的一种而不是另一种?您的哪些经历激发了您的观点?
The checks Selenium performs usually come in two flavours: assertFoo and verifyFoo. I understand that assertFoo fails the whole testcase whereas verifyFoo just notes the failure of that check and lets the testcase carry on.
So with verifyFoo I can get test results for multiple conditions even if one of them fails. On the other hand, one failing check for me is enough to know, that my edits broke the code and I have to correct them anyway.
In which concrete situations do you prefer one of the two ways of checking over the other? What are your experiences that motivate your view?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我将使用
assert()
作为测试的入口点(“网关”)。仅当断言通过时,才会执行verify()
检查。例如,如果我要检查由一系列操作产生的窗口的内容,我会assert()
窗口是否存在,然后verify()
内容。我经常使用的一个例子 - 检查 jqgrid 中的估计:
assert()
网格是否存在,以及verify()
估计。I would use an
assert()
as an entry point (a "gateway") into the test. Only if the assertion passes, will theverify()
checks be executed. For instance, if I'm checking the contents of a window resulting from a series of actions, I wouldassert()
the presence of the window, and thenverify()
the contents.An example I use often - checking the estimates in a jqgrid:
assert()
the presence of the grid, andverify()
the estimates.我遇到了一些问题,这些问题可以通过使用来克服
,
例如,在表单验证中,如果您想检查表单元素,可以使用
will just pass the test even if the string is not present in the form.
如果将断言替换为验证,那么它会按预期工作。
我强烈建议使用
assert*()
。I've come across a few problems which were overcome by using
instead of
For example, in form validations if you want to check a form element, the use of
will just pass the test even if the string is not present in the form.
If you replace assert with verify, then it works as expected.
I strongly recommend to go with using
assert*()
.如果您在生产系统上运行 Selenium 测试并希望确保您以测试用户身份登录(例如,而不是您的个人帐户),那么最好在触发任何操作之前首先断言正确的用户已登录。如果不小心使用,可能会产生意想不到的效果。
If you are running Selenium tests on a production system and want to make sure you are logged-in as a test user e.g., instead of your personal account, it is a good idea to first assert that the right user is logged in before triggering any actions that would have unintended effects, if used by accident.
通常,您应该坚持每个测试用例一个断言,在这种情况下,差异归结为必须运行的任何拆卸代码。但无论如何,您应该将其放入
@After
方法中。我在 SeleneseTestBase 中的
verify*()
方法上遇到了很多问题(例如,它们使用System.out.println()
和com.thoughtworks .selenium.SeleneseTestBase.assertEquals(Object, Object)
只是没有达到您的预期),所以我已经停止使用它们。Usually you should stick to one assertion per test case, and in this case the difference boils down to any tear-down code which must be run. But you should probably put this in an
@After
method anyway.I've had quite a few problems with the
verify*()
methods in SeleneseTestBase (e.g. they useSystem.out.println()
, andcom.thoughtworks.selenium.SeleneseTestBase.assertEquals(Object, Object)
just doesn't do what you expect) so I've stopped using them.