User:Babbage Linden/Django/Automated Integration Tests

From Second Life Wiki
Jump to navigation Jump to search

Django has an automated integration test framework that creates test databases, populates them with text fixtures, runs tests and cleans up the test database. The test framework extends the python unit test frame providing a flexible test client that allows tests to be written like this:

import os

from django.test import TestCase

from indra.base import llsd

def llsd_array_contains(array, substring):
    return [elem for elem in array if substring in elem]

class RegionServicesReadTest(TestCase):
    fixtures = [os.path.dirname(__file__) + '/indra.json']

    def test001_get_null_region_id_returns_404(self):
        uri = '/region/42000000-0000-0000-0000-000000000042/'
        response = self.client.get(uri, {}, HTTP_ACCEPT='application/llsd+xml')
        self.failUnlessEqual(response.status_code, 404)

    def test002_get_valid_region_id_returns_200(self):
        uri = '/region/78bc5b1a-f183-45c2-a6d0-2dcec6301e80/'
        response = self.client.get(uri, {}, HTTP_ACCEPT='application/llsd+xml')
        self.failUnlessEqual(response.status_code, 200)

And run like this:

$ python manage.py test
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
Creating table django_admin_log
Creating table estate
Creating table presence_simulator
Creating table simulator
Installing index for auth.Permission model
Installing index for auth.Message model
Installing index for admin.LogEntry model
................................
----------------------------------------------------------------------
Ran 32 tests in 1.891s

OK
Destroying test database...