akaihola / nose-django

This is a plugin for nosetests to run unit tests against Django. It's been tested against Django 1.0b1 and nose 0.10. This repository is a branch off of http://www.assembla.com/wiki/show/cvoCb8Dqqr3yW...

Clone this repository (size: 28.4 KB): HTTPS / SSH
$ hg clone http://bitbucket.org/akaihola/nose-django/

Changed (Δ2.0 KB):

raw changeset »

examples/README.TXT (8 lines added, 4 lines removed)

examples/project/settings.py (7 lines added, 1 lines removed)

examples/project/templates/500.html (1 lines added, 0 lines removed)

examples/project/tests/__init__.py

examples/project/tests/test_views.py

examples/project/zoo/test_urlconf.py (38 lines added, 0 lines removed)

examples/project/zoo/tests.py (0 lines added, 2 lines removed)

nosedjango/nosedjango.py (17 lines added, 0 lines removed)

Up to file-list examples/README.TXT:

@@ -27,7 +27,8 @@ A successful run should hit *11* test ca
27
27
--- Sample test run below ---
28
28
29
29
C:\dev\nosedjango\examples\project>nosetests -v --with-django --with-doctest --doctest-tests --doctest-tests
30
doctest: project.zoo.models.zoo ... ok
30
31
Doctest: project.zoo.models.Zoo ... ok
31
32
Doctest: project.zoo.models.Zoo.__str__ ... ok
32
33
Doctest: project.zoo.models.func ... ok
33
34
This is just a stub for a regular test method ... ok
@@ -37,12 +38,15 @@ project.zoo.test_fixtures.TestFixture1.t
37
38
project.zoo.test_fixtures.TestFixture2.test_count ... ok
38
39
project.zoo.test_race.TestDBRace1.test1 ... ok
39
40
project.zoo.test_race.TestDBRace2.test1 ... ok
40
project.tests.test_views.test_view_index ... ok
41
We're customizing the ROOT_URLCONF with zoo.urls, ... ok
42
We're using the standard ROOT_URLCONF, so we need to ... ok
43
testcase1 (project.zoo.tests.TestDjango) ... ok
44
testcase2 (project.zoo.tests.TestDjango) ... ok
41
45
42
46
----------------------------------------------------------------------
43
Ran 11 tests in 0.859s
47
Ran 14 tests in 1.219s
44
48
45
49
OK
46
50
Destroying test database...
47
51
48
C:\dev\nosedjango\examples\project>
52

Up to file-list examples/project/settings.py:

1
1
# Django settings for 'project' project.
2
2
3
import os
4
PROJECT_PATH = os.path.abspath(os.path.split(__file__)[0])
5
3
6
DEBUG = True
4
7
TEMPLATE_DEBUG = DEBUG
5
8
@@ -10,7 +13,7 @@ ADMINS = (
10
13
MANAGERS = ADMINS
11
14
12
15
DATABASE_ENGINE = 'sqlite3'           # 'postgresql', 'mysql', 'sqlite3' or 'ado_mssql'.
13
DATABASE_NAME = 'zoo.db'             # Or path to database file if using sqlite3.
16
DATABASE_NAME = os.path.join(PROJECT_PATH, 'zoo.db')  # Or path to database file if using sqlite3.
14
17
DATABASE_USER = ''             # Not used with sqlite3.
15
18
DATABASE_PASSWORD = ''         # Not used with sqlite3.
16
19
DATABASE_HOST = ''             # Set to empty string for localhost. Not used with sqlite3.
@@ -67,6 +70,7 @@ MIDDLEWARE_CLASSES = (
67
70
ROOT_URLCONF = 'project.urls'
68
71
69
72
TEMPLATE_DIRS = (
73
    os.path.join(PROJECT_PATH, 'templates'),
70
74
    # Put strings here, like "/home/html/django_templates" or "C:/www/django/templates".
71
75
    # Always use forward slashes, even on Windows.
72
76
    # Don't forget to use absolute paths, not relative paths.
@@ -80,3 +84,5 @@ INSTALLED_APPS = (
80
84
    'django.contrib.sites',
81
85
    'project.zoo'
82
86
)
87
handler500 = 'django.views.defaults.server_error'
88

Up to file-list examples/project/templates/500.html:

1
blah.  HTTP 500 error

Up to file-list examples/project/zoo/test_urlconf.py:

1
from django.test import TestCase
2
from django.test.client import Client
3
4
class TestStandardUrlConf(TestCase):
5
    def test_index(self):
6
        '''
7
        We're using the standard ROOT_URLCONF, so we need to
8
        pass in /zoo/, just the empty string
9
        '''
10
        c = Client()
11
        resp = c.get('')
12
        assert resp.status_code == 500
13
14
        c = Client()
15
        resp = c.get('/zoo/')
16
        assert "Just a title" in resp.content
17
        assert "foobar" in resp.content
18
19
class TestCustomUrlConf(TestCase):
20
    urls = 'zoo.urls'
21
22
    def test_index(self):
23
        '''
24
        We're customizing the ROOT_URLCONF with zoo.urls,
25
        so we do *not* need to pass in /zoo/, just the empty string
26
        '''
27
        c = Client()
28
        resp = c.get('')
29
        assert "Just a title" in resp.content
30
        assert "foobar" in resp.content
31
32
        c = Client()
33
        resp = c.get('/zoo/')
34
        assert resp.status_code == 500
35
36
37
38

Up to file-list examples/project/zoo/tests.py:

@@ -5,8 +5,6 @@ class TestDjango(TestCase):
5
5
    def testcase1(self):
6
6
        zoo = Zoo.objects.create(name='blah')
7
7
        assert Zoo.objects.count() == 1
8
        import pdb
9
        pdb.set_trace()
10
8
11
9
    def testcase2(self):
12
10
        zoo = Zoo.objects.create(name='blah')

Up to file-list nosedjango/nosedjango.py:

@@ -114,6 +114,7 @@ class NoseDjango(Plugin):
114
114
115
115
        from django.core.management import call_command
116
116
        from django.core.urlresolvers import clear_url_caches
117
        from django.conf import settings
117
118
        call_command('flush', verbosity=0, interactive=False)
118
119
119
120
        if isinstance(test, nose.case.Test) and \
@@ -122,6 +123,16 @@ class NoseDjango(Plugin):
122
123
                # We have to use this slightly awkward syntax due to the fact
123
124
                # that we're using *args and **kwargs together.
124
125
                call_command('loaddata', *test.context.fixtures, **{'verbosity': 0}) 
126
127
        if isinstance(test, nose.case.Test) and \
128
            isinstance(test.test, nose.case.MethodTestCase) and \
129
            hasattr(test.context, 'urls'):
130
                # We have to use this slightly awkward syntax due to the fact
131
                # that we're using *args and **kwargs together.
132
                self.old_urlconf = settings.ROOT_URLCONF
133
                settings.ROOT_URLCONF = self.urls
134
                clear_url_caches()
135
125
136
        self.mail.outbox = []
126
137
127
138
    def finalize(self, result=None):
@@ -134,5 +145,11 @@ class NoseDjango(Plugin):
134
145
135
146
        from django.test.utils import teardown_test_environment
136
147
        from django.db import connection
148
        from django.conf import settings
137
149
        connection.creation.destroy_test_db(self.old_db, verbosity=self.verbosity)   
138
150
        teardown_test_environment()
151
152
        if hasattr(self, 'old_urlconf'):
153
            settings.ROOT_URLCONF = self.old_urlconf
154
            clear_url_caches()
155