Package: homophony
Abouthomophonyprovideszc.testbrowserintegration for Django;zc.testbrowseris a lot more robust than the default functional testing
client that comes with Django.See theintroductiontozc.testbrowserfor a better understanding
of how powerful it is.Using homophonyFirst of all, you need to havehomophonyinstalled; for your convenience,
recent versions should be available from PyPI.Let’s say you’re working on an application calledfoobar; the tests for
this application are located infoobar/tests.py.Unit testsUse this as a starting point forfoobar/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 customsetUpandtearDownmethods
should involve calling those defined inBrowserTestCase.DoctestsIf you prefer doctests over unit tests (as we do!), use the following as a base
forfoobar/tests.py:from homophony import DocFileSuite

def suite():
    return DocFileSuite('tests.txt')And here is an examplefoobar/tests.txtfile: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
    'Home'HelpersThere are some useful helpers on the browser class.  You can runXPathqueries
on HTML documents usingqueryHTML, like this:>>> browser.queryHTML('//h1')
<h1>Hello Jim</h1>When debugging tests, it is sometimes handy to open a browser at a particular
point in the test.  You can accomplish that by invokingserve:>>> browser.serve()This command will start an HTTP server and open a web browser with live access
to your application.  Use Ctrl-C to stop the server and continue running tests.There is a known issue that the mini-webserver does not serve static files, so
your browser may not be able to access Javascript or CSS used by your app.The browser will persist cookies accross requests, so things like user sessions
should work.Example applicationThere 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-v2parameter 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 forurllibto pass all requests forhttp://testserverto a subclass ofWSGIHandler(which
exposes Django applications throughWSGI). The real heavy lifting is
performed bywsgi_intercept.FeedbackThere is ahome pagewith
instructions on how to access the code repository.Send feedback and suggestions toteam@shrubberysoft.com.