All articles, tagged with “tests”

TDD with Django. Part 2. Twill tests

Testing of the web projects is very hard. We try to cover all application with unit tests and twill tests. And if we have a lot of javascript, we also make selenium tests.

To use twill tests from the nosetests and Django, we’ve created simple helper to create many tests faster. Here is it:

 # -*- coding: utf-8 -*-
 IP = '127.0.0.1'
 PORT = 8088
 SITE = 'http://%s:%s' % (IP, PORT)

 class TwillMock(object):

    def setup(self):
        '''
        — setup twill virtual web server
        '''
        from django.core.servers.basehttp import AdminMediaHandler
        from django.core.handlers.wsgi import WSGIHandler

        app = AdminMediaHandler(WSGIHandler())
        add_wsgi_intercept(IP, PORT, lambda: app)

Making new test class derived from the TwillMock class make creation of nose twill tests much easier.
Look at the example of such test:

class TestLogin(TwillMock):

    def test_start_page(self):
        """
        Test start_page view
        """
        go(SITE)
        code(200)
        check_links()

    def test_login(self):
        go(SITE + '/accounts/login/')
        code(200)
        show()
        formvalue(1, 'username',  'test1')
        formvalue(1, 'password', 'test1')
        submit()
        code(200)