18

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?

1
  • Selenium webdriver api or junit has nothing called "verify". Selenium IDE has verify.
    – MasterJoe
    Nov 6, 2016 at 22:08

4 Answers 4

16

I would use an assert() as an entry point (a "gateway") into the test. Only if the assertion passes, will the verify() checks be executed. 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.

An example I use often - checking the estimates in a jqgrid: assert() the presence of the grid, and verify() the estimates.

0
3

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*().

1
  • I assume you meant assertTrue() in the third example?
    – medonja
    Sep 2, 2015 at 22:03
2

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.

1

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 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.

3
  • But having only one assertion per testcase makes testing super slow, because selenium has to start and stop a browser instance for every single assertion (which can use up to 15 sec per testcase on my laptop). How do you solve that problem?
    – jammon
    Apr 22, 2011 at 7:38
  • 2
    I share the selenium instance between tests, see stackoverflow.com/questions/5626103/…. But to be fair, I also have plenty of tests with multiple asserts, e.g. when I'm testing that a form had all its fields updated correctly.
    – artbristol
    Apr 25, 2011 at 15:59
  • What is verify ? I can't find any api for verify in junit.
    – MasterJoe
    Sep 30, 2016 at 0:07

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.