[wdmmg-dev] Use test browser in functional tests

Carsten Senger senger at rehfisch.de
Fri May 20 09:51:44 UTC 2011


I'd like to have a testbrowser in functional tests with which I can access
links and forms without the need to do text matching in the respose body.

Background:
I changed the entity urls to use mongodb uids. There where some urls that I
did not notice they need to change in my first tests. I wrote
functional tests to cover them. They look like that::

  entity_url = url(controller='entity',
                   id=str(entity['_id']),
                   slug='test-entity-label',
                   action='view')
  response = self.app.get()
  self.assertTrue('<b>1 entries</b> found.<br />' in response)
  self.assertTrue('entries.json">' in response)
  self.assertTrue('entries.csv">' in response)

It matches parts of hrefs to .../entires.json and .../entires.csv
I would like to see if these links work. I could start to build more urls
in the tests and try them out or use regular expressions, but in both cases
I would to put more knowledge about how these other parts work into the
functional test for the entity pages.

When I worked with Zope/Plone I used zope.testbrowser[1] a lot.
It provides a Browser class with an API to find html elements e.g. with the
containing text, the id or the class. You can fill out forms and click
buttons [2] or links[3]:

  entity_url = url(controller='entity',
                   id=str(entity['_id']),
                   slug='test-entity-label',
                   action='view')
  browser = Browser()
  browser.open(entity_url)
  entries_link = browser.getLink(url='/entries.json') # match partial url
  entries_json = entries_link.click()
  self.assertEqual(browser.headers['status'], '200 OK')

zope.testbrowser wraps the mechanize library into a friedly api and
supports wsgi applications. There are probably other libraries out there
that work similarly.
What it does not do is drive a real browser, like windmill or selenium, and
hence is not able to test javascript. But integrates nicely with unittests
and is easier to setup than selenium.

I'd like to add zope.testbrowser as a extra_requires['tests'] and integrate
it into the test setup.

..Carsten


[1] <http://pypi.python.org/pypi/zope.testbrowser>
[2] <http://pypi.python.org/pypi/zope.testbrowser#forms>
[3] 
<http://pypi.python.org/pypi/zope.testbrowser#navigation-and-link-objects>




More information about the openspending-dev mailing list