简体   繁体   English

Selenium 中的“断言”与“验证”

[英]'assert' vs. 'verify' in Selenium

The checks Selenium performs usually come in two flavours: assertFoo and verifyFoo. Selenium 执行的检查通常有两种形式:assertFoo 和 verifyFoo。 I understand that assertFoo fails the whole testcase whereas verifyFoo just notes the failure of that check and lets the testcase carry on.我知道 assertFoo 使整个测试用例失败,而 verifyFoo 只是指出该检查的失败并让测试用例继续进行。

So with verifyFoo I can get test results for multiple conditions even if one of them fails.因此,使用 verifyFoo 我可以获得多个条件的测试结果,即使其中之一失败。 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?你的哪些经历激发了你的观点?

I would use an assert() as an entry point (a "gateway") into the test.我将使用assert()作为测试的入口点(“网关”)。 Only if the assertion passes, will the verify() checks be executed.只有当断言通过时, verify()检查才会被执行。 For instance, if I'm checking the contents of a window resulting from a series of actions, I would assert() the presence of the window, and then verify() the contents.例如,如果我正在检查由一系列操作产生的窗口内容,我会assert()窗口的存在,然后verify()内容。

An example I use often - checking the estimates in a jqgrid: assert() the presence of the grid, and verify() the estimates.我经常使用的一个例子 - 检查 jqgrid 中的估计值: assert()网格的存在,和verify()估计值。

I've come across a few problems which were overcome by using我遇到了一些通过使用克服的问题

assert*()

instead of代替

verify*()

For example, in form validations if you want to check a form element, the use of例如,在表单验证中,如果你想检查一个表单元素,使用

verifyTrue(...);
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*() .我强烈建议使用assert*()

如果您在生产系统上运行 Selenium 测试并希望确保您以测试用户身份登录,而不是您的个人帐户,那么在触发任何如果意外使用会产生意想不到的效果的动作。

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.但是无论如何你应该把它放在@After方法中。

I've had quite a few problems with the verify*() methods in SeleneseTestBase (eg they use System.out.println() , and com.thoughtworks.selenium.SeleneseTestBase.assertEquals(Object, Object) just doesn't do what you expect) so I've stopped using them.我在 SeleneseTestBase 中的verify*()方法有很多问题(例如,它们使用System.out.println() ,而com.thoughtworks.selenium.SeleneseTestBase.assertEquals(Object, Object)只是不做你期望)所以我已经停止使用它们。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM