Test

The test module defines a new test client, base testcase, testing mixins, and an alternate test runner.

Test Client

hilbert.test.Client is a simple extension of the Django test client which allows for an extra argument in get and post called is_ajax. This will default to False but when passed as True it will make the request as an AJAX request.

TestCase

hilbert.test.TestCase is an extension of the Django TestCase which uses the above test client and defines some helpful methods.

TestCase.get_random_string(length=10, choices=string.ascii_letters)

This method is used to generate random string data used in various tests.

Parameters:
  • length – The length of the string to return
  • choices – The character set from which to draw the string characters
Returns:

A random string

TestCase.get_random_email(domain=u'example.com')

This method is used to generate random email for the given domain.

Parameters:domain – The domain name for the email address
Returns:A random email address as a string
TestCase.create_user(data=None)

This generates a new django.contrib.auth.User. If no data is given then the user will be given a random username, email, and password.

Parameters:data – A dictionary of data for the user. Allowed keys: username, password, email
Returns:A newly created User model

CoverageRunner

The CoverageRunner is a new test runner based on snippets 705 and 2052. It uses Ned Batchelder’s coverage.py to determine the percent of code executed by the test suite. It can be enabled by setting TEST_RUNNER=’hilbert.test.CoverageRunner’ in your Django settings file. You must also define a set of submodules to be included in the report using the setting COVERAGE_MODULES.

COVERAGE_MODULES = (
    'decorators',
    'http',
    'forms',
    'models',
    'views',
)

Using this setting the test runner will report the coverage of listed submodules of the tested apps (if they exist).

New in version 0.4.

If you usually want to pass the same set of test labels when you run tests, you can set DEFAULT_TEST_LABELS in your settings.

DEFAULT_TEST_LABELS = ['app1', 'app2.TestClass', 'app3.TestClass.test_method']

Then django-admin.py test will act like

django-admin.py test app1 app2.TestClass app3.TestClass.test_method

If you’ve done that, you can still pass ‘all’ on the command line to run tests as if you had not passed any test labels, e.g. run test all.

ViewTestMixin

This is a testing mixin to help writing tests for your Django views. It will automatically reverse the data returned by get_url() and attach it to self.url. It also contains one test which does a GET request on the url.

New in version 0.3.

The ViewTestMixin changed in version 0.3 to expect a class attribute url_name.

class DashboardTestCase(TestCase, ViewTestMixin):
    url_name = 'dashboard'

If your url needs either args or kwargs you can override get_url_args or get_url_kwargs.

AuthViewMixin

AuthViewMixin extends the ViewTestMixin for testing views which require authentication. It automatically creates a user and signs them in for any requests. It adds an additional test to ensure that authentication is required. This must be used in conjunction with hilbert.test.TestCase.

Project Versions

Table Of Contents

Previous topic

Middleware

Next topic

Available Settings

This Page