Assert vs. Verify in Se-RC

So in Se there is the notion of a Assert and a Verify. To recap, when an Assert fails, then the script stops immediately and goes to the teardown function. A Verify will ultimately fail the script, but the test will continue.

In Se-IDE the commands make the syntax very obvious but it is less so in Se-RC. Here is a python example to illustrate how to use both.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
import unittest
 
class assertExample(unittest.TestCase):
 
    def setUp(self):
        self.verificationErrors = []
        self.selenium = selenium("localhost", 4444, "*chrome", "http://you.site/")
        self.selenium.start()
        self.open("/")
        self.wait_for_page_to_load("10000")
 
    def testSomething(self):
      se = self.selenium
      se.assertEqual("Login", se.get_title())
      try:
          self.assertEqual("Please login", se.is_element_present("welcome_message"))
      except AssertionError, e:
          self.verificationErrors.append(str(e))
 
    def tearDown(self):
        self.selenium.stop()
        self.assertEqual([], self.verificationErrors)
 
if __name__ == "__main__":
    unittest.main()

First let’s look at the Assert. In line 14 of the above example I’m using a common pattern of Asserting upon page load that I am on the correct page. If the title is not exactly Login, then the script will end and the tearDown function will execute. The rationale here is that if the script has taken you to a page that you are not expecting, then the rest of the script’s results will be suspect — if able to continue at all.

Lines 15 – 18 are the Verify. Notice how it still is based on an Assert provided by unittest, but we are catching the AssertionError that gets thrown on failure. This prevents the script from running and allows us to track the failure to the list that was created on line 6. The list contents are what will determine whether a test passes (empty) or fails (non-empty) which is done in the tearDown (line 22).

Is this the only place to use an Assert or Verify? Of course not, its just a common one. I also use Asserts when something is critical to the purpose of the script (the pass / fail criteria) or when something is worthy of extra special attention. The trick is to use both in a way that makes sense for your application and scripts.

Post a Comment

Your email is never published nor shared. Required fields are marked *