Login

django-noserun for testing

Author:
mjt
Posted:
March 22, 2009
Language:
Python
Version:
1.0
Score:
1 (after 1 ratings)

I wanted to use Nose with Django so I came up with this.

TEST_RUNNER = 'noserun.run_tests' in settings.py

It does not do setup/teardown implicitly between test methods, you need to call nosetest.test.flush() and nosetest.test.loaddata() manually if you want that.

Enables the method names setup and teardown

The environment variable NOSE_COVER runs coverage tests and NO_DROPDB preserves the test db.

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
"""
noserun/__init__.py here
"""

# vim: tabstop=4 expandtab autoindent shiftwidth=4 fileencoding=utf-8

import os
import sys
if not os.environ.has_key('DJANGO_SETTINGS_MODULE'): 
    print 'DJANGO_SETTINGS_MODULE not set'
    sys.exit(1)

from django.conf import settings

from django.core.management.commands import syncdb   

from django.db import connection

from django.test import utils

import nose

def run_tests(test_labels, verbosity=2, interactive=True, extra_tests=[]):
    """Run tests with nose
    """

    ### Preamble
    # Remember our real database name for restore-after-destruct
    real_db = settings.DATABASE_NAME
    cmd_dict = {
        'verbosity': verbosity,
        'interactive': interactive,
    }

    ### The real deal
    ## Setup
    utils.setup_test_environment()

    ## Create test db
    test_db = connection.creation.create_test_db(verbosity)
    if verbosity:
        print 'Created db %s' % test_db

    # syncdb
    cmd = syncdb.Command()
    cmd.handle_noargs(**cmd_dict)

    ## Find tests
    # Not needed with nose <3

    ## Run tests
    # Command line handling
    argv = sys.argv
    if verbosity == 2:
        argv.append('-s')

    # Always do doctests, because Django does them too!
    # At least I believe/hope it does ;)
    argv.append('--with-doctest')

    # manage.py doesn't like arbitrary args, use environment
    if os.environ.has_key('NOSE_COVER'):
        argv.append('--with-coverage')

    if verbosity == 2:
        print 'Running nose'
        print '    Test labels', test_labels
        print '    Extra tests', extra_tests

    # Pass argv explicitly here
    # The above assignment sets a reference and Just Works,
    # but this makes it more obvious
    if test_labels:
        for label in test_labels:
            nose.run(module=label, argv=argv)
    else:
        nose.run(argv=argv)

    ## Destroy test db
    if not os.environ.has_key('NO_DROPDB'):
        connection.creation.destroy_test_db(real_db, verbosity)
    if verbosity:
        print 'OK'

    ## Teardown
    utils.teardown_test_environment()


# EOF

"""
noserun/test.py here
"""

# vim: tabstop=4 expandtab autoindent shiftwidth=4 fileencoding=utf-8

from django.core.management import call_command

from django.core import mail

from nose.util import try_run

from django import test

class TestCase(test.TestCase):
    """Sane TestCase for nose
    """

    def setUp(self):
        names = ('setup',)
        try_run(self, names)

    def tearDown(self):
        names = ('teardown',)
        try_run(self, names)

    def _pre_setup(self):
        """Otherwise the superclass _pre_setup but skip everything
        related to fixtures.
        """

        if hasattr(self, 'urls'):
            self._old_root_urlconf = settings.ROOT_URLCONF
            settings.ROOT_URLCONF = self.urls
            clear_url_caches()
        mail.outbox = []

def flush(verbosity=0, interactive=False):
    """Shortcut to flush db
    """

    call_command('flush', verbosity=verbosity, interactive=interactive)

def loaddata(*fixtures, **kwargs):
    """A list of fixtures to load, kwargs given as **{'verbosity': 0}
    and defaults to exactly that
    """

    if not kwargs:
        kwargs = {'verbosity': 0}
    call_command('loaddata', *fixtures, **kwargs)

# EOF

More like this

  1. Template tag - list punctuation for a list of items by shapiromatron 11 months, 3 weeks ago
  2. JSONRequestMiddleware adds a .json() method to your HttpRequests by cdcarter 11 months, 4 weeks ago
  3. Serializer factory with Django Rest Framework by julio 1 year, 6 months ago
  4. Image compression before saving the new model / work with JPG, PNG by Schleidens 1 year, 7 months ago
  5. Help text hyperlinks by sa2812 1 year, 8 months ago

Comments

Please login first before commenting.