Skip to main content

Django and zc.testbrowser integration

Project description

About

homophony provides zc.testbrowser integration for Django; zc.testbrowser is a lot more robust than the default functional testing client that comes with Django.

See the introduction to zc.testbrowser for a better understanding of how powerful it is.

Using homophony

First of all, you need to have homophony installed; for your convenience, recent versions should be available from PyPI.

Let’s say you’re working on an application called foobar; the tests for this application are located in foobar/tests.py.

Unit tests

Use this as a starting point for foobar/tests.py:

from homophony import BrowserTestCase, Browser

class FoobarTestCase(BrowserTestCase):

    def testHome(self):
        browser = Browser()
        browser.open('http://testserver')
        browser.getControl(name='first_name').value = 'Jim'
        browser.getForm().submit()
        self.assertEquals(browser.url, 'http://testserver/hello')
        self.assertEquals(browser.title, 'Hello Jim')

Bear in mind that implementing custom setUp and tearDown methods should involve calling those defined in BrowserTestCase.

Doctests

If you prefer doctests over unit tests (as we do!), use the following as a base for foobar/tests.py:

from homophony import DocFileSuite

def suite():
    return DocFileSuite('tests.txt')

And here is an example foobar/tests.txt file:

The website welcomes its visitors with a form:

    >>> browser = Browser()
    >>> browser.open('http://testserver')
    >>> browser.getControl(name='first_name').value = 'Jim'
    >>> browser.getForm().submit()

When a name is given, it echoes back with an informal greeting:

    >>> browser.title
    'Hello Jim'
    >>> print browser.contents
    <!DOCTYPE html>
    ...
    <h1>Hello Jim</h1>
    ...

And there is a link to go back:

    >>> browser.getLink('Go back').click()
    >>> browser.title
    'Homez'

Example application

There is an example Django application in the source distribution. Let’s run the tests:

wormhole:example admp$ ./manage.py test -v 2 website
Creating test database...
Creating table auth_permission
Creating table auth_group
Creating table auth_user
Creating table auth_message
Creating table django_content_type
Creating table django_session
Creating table django_site
Installing index for auth.Permission model
Installing index for auth.Message model
...
testHome (example.website.tests.FoobarTestCase) ... ok
Doctest: tests.txt ... ok

----------------------------------------------------------------------
Ran 2 tests in 0.102s

OK
Destroying test database...

The -v 2 parameter is there to get the list of tests printed, and is otherwise unnecessary.

For learning purposes, try to break the tests and witness the details in the output of the test runner.

How does it work?

Custom hooks are installed for urllib to pass all requests for http://testserver to a subclass of WSGIHandler (which exposes Django applications through WSGI). The real heavy lifting is performed by wsgi_intercept.

Feedback

There is a home page with instructions on how to access the code repository.

Send feedback and suggestions to team@shrubberysoft.com.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

homophony-0.1.2.tar.gz (4.8 kB view details)

Uploaded Source

File details

Details for the file homophony-0.1.2.tar.gz.

File metadata

  • Download URL: homophony-0.1.2.tar.gz
  • Upload date:
  • Size: 4.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for homophony-0.1.2.tar.gz
Algorithm Hash digest
SHA256 7a796d2c0994dc0da7978a511cf3eea09da8b5d51e646d41313124cf44768951
MD5 609e4e4c0dc643d4ee1bee0b4288a12e
BLAKE2b-256 34a3d6d3276914f8acbff78a51ba7ca922cccae907816e28d30431cd0d8f7a3c

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page