All articles, tagged with “tdd”

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)

TDD with Django. Part 1

Starting my first big Django-project (Building.ua), I hadn’t know anything about TDD except the fact it is. Project was done but it is very buggy and hard for future develop.
After very productive work with Andrey Khavryuchenko , I’ve learnt his model of tests and now try to add some points to it to make it cover near 100% of the Django application.

All tests in Django-TDD I’d like to divide into parts:
— Unit tests
— Integration and modification tests
— Twill html tests
— Selenium tests
— Other specific tests (e.g. test RTF or PDF output)

Andrey use nosetest in all python projects. And this model is better for me as standard Djando UnitTest derived testrunner. The biggest issue of such model is that you can use one test templates for all your python projects. Second, we use nosetest with coverage plugin which give us very useful metric to know how code is tested.

Unit tests for every new Django model looks like:
— create/edit/delete object of the model
— call unicode() and get_absolute_url() methods

Extensions of the model results appearing new tests in the testscase for this model.

All unit and integration django tests we create as the subclasses of the DbMock class. As a result every test has it’s own clean database and we don’t care about it.

For testing simple html functions and interactions we use Twill. Twill give the possibility to run by the pages, find and fill the forms and check results. Of course, you could use Selenium for this too, but in this case Twill is more cheaper solution.

To run Twill (and Selenium), we create virtual Django WSGI server and connect to it from the tests.


For testing javascript iterations, we use Selenium-RC, which have good python interface. The biggest problem for Selenium tests — is to run this tests on the buildbot servers. The one solution I see — is to setup server with X and set Selenium to run Firefox on this server.

I am planning to write some code in next articles to show our examples for running nose/twill/selenium tests for Django application.