Skip to content

Commit e144e43

Browse files
committed
Refactored tests to use django-discover-runner and not an own testproject anymore.
1 parent a8b158a commit e144e43

16 files changed

+92
-130
lines changed

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,5 +34,5 @@
3434
packages=find_packages(exclude=['tests']),
3535
include_package_data=True,
3636
test_suite='tests.runtests.main',
37+
test_requires=['django-discover-runner'],
3738
)
38-
File renamed without changes.

tests/runtests.py

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,15 @@
11
#!/usr/bin/env python
2-
3-
"""Borrowed from Carl Meyer's django-adminfiles."""
4-
52
import os
63
import sys
4+
from django.core.management import call_command
75

8-
testapp_dir = os.path.dirname(os.path.abspath(__file__))
9-
constance_dir = os.path.dirname(testapp_dir)
10-
sys.path.insert(0, constance_dir)
11-
sys.path.insert(0, testapp_dir)
6+
os.environ['DJANGO_SETTINGS_MODULE'] = 'tests.settings'
127

13-
os.environ['DJANGO_SETTINGS_MODULE'] = 'testproject.settings'
14-
15-
from django.test.simple import DjangoTestSuiteRunner
168

179
def main():
18-
runner = DjangoTestSuiteRunner()
19-
failures = runner.run_tests(['test_app'], verbosity=1, interactive=True)
20-
sys.exit(failures)
10+
result = call_command('test', 'tests', verbosity=2)
11+
sys.exit(result)
12+
2113

2214
if __name__ == '__main__':
2315
main()

tests/testproject/settings.py renamed to tests/settings.py

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,21 @@
11
# -*- encoding: utf-8 -*-
2-
2+
import os
33
from datetime import datetime, date, time
44
from decimal import Decimal
55

6+
# using the parent directory as the base for the test discovery
7+
TEST_DISCOVER_TOP_LEVEL = os.path.join(os.path.dirname(__file__), '..')
8+
9+
TEST_RUNNER = 'discover_runner.DiscoverRunner'
10+
611
SECRET_KEY = 'cheese'
712

13+
DATABASE_ENGINE = 'sqlite3'
14+
815
DATABASES = {
916
'default': {
1017
'ENGINE': 'django.db.backends.sqlite3',
18+
'NAME': ':memory:',
1119
}
1220
}
1321

@@ -16,16 +24,15 @@
1624
'django.contrib.auth',
1725
'django.contrib.contenttypes',
1826
'django.contrib.admin',
27+
'south',
1928

2029
'constance',
2130
'constance.backends.database',
22-
23-
'testproject.test_app',
2431
)
2532

26-
ROOT_URLCONF = 'testproject.urls'
33+
ROOT_URLCONF = 'tests.urls'
2734

28-
CONSTANCE_CONNECTION_CLASS = 'testproject.test_app.redis_mockup.Connection'
35+
CONSTANCE_CONNECTION_CLASS = 'tests.redis_mockup.Connection'
2936

3037
CONSTANCE_CONFIG = {
3138
'INT_VALUE': (1, 'some int'),

tests/testproject/test_app/tests/test_config.py renamed to tests/storage.py

Lines changed: 1 addition & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,8 @@
11
# -*- encoding: utf-8 -*-
2-
32
import sys
43
from datetime import datetime, date, time
54
from decimal import Decimal
65

7-
from django.test import TestCase
8-
from django.conf import settings
9-
from django.contrib import admin
10-
from django.contrib.auth.models import User
11-
12-
from constance import settings
13-
from constance.admin import Config
14-
15-
# Use django RequestFactory later on
16-
from testproject.test_app.tests.helpers import FakeRequest
17-
186

197
class TestStorage(object):
208

@@ -71,24 +59,7 @@ def test_nonexistent(self):
7159
pass
7260
self.assertEquals(type(e), AttributeError)
7361

74-
class TestRedis(TestCase, TestStorage):
75-
76-
def setUp(self):
77-
self.old_backend = settings.BACKEND
78-
settings.BACKEND = 'constance.backends.redisd.RedisBackend'
79-
del sys.modules['constance']
80-
from constance import config
81-
config._backend._rd.clear()
82-
83-
def tearDown(self):
84-
del sys.modules['constance']
85-
from constance import config
86-
config._backend._rd.clear()
87-
settings.BACKEND = self.old_backend
88-
import constance
89-
constance.config = Config()
90-
91-
def testMissingValues(self):
62+
def test_missing_values(self):
9263
from constance import config
9364

9465
# set some values and leave out others
@@ -110,30 +81,3 @@ def testMissingValues(self):
11081
self.assertEquals(config.FLOAT_VALUE, 3.1415926536) # this should be the default value
11182
self.assertEquals(config.DATE_VALUE, date(2001, 12, 20))
11283
self.assertEquals(config.TIME_VALUE, time(1, 59, 0))
113-
114-
115-
class TestDatabase(TestCase, TestStorage):
116-
117-
def setUp(self):
118-
self.old_backend = settings.BACKEND
119-
settings.BACKEND = 'constance.backends.database.DatabaseBackend'
120-
121-
def tearDown(self):
122-
del sys.modules['constance']
123-
settings.BACKEND = self.old_backend
124-
import constance
125-
constance.config = Config()
126-
127-
class TestAdmin(TestCase):
128-
model = Config
129-
130-
def setUp(self):
131-
self.user = User.objects.create_superuser('admin', 'nimda', '[email protected]')
132-
self.options = admin.site._registry[self.model]
133-
self.fake_request = FakeRequest(user=self.user)
134-
self.client.login(username=self.user, password='nimda')
135-
136-
def test_changelist(self):
137-
response = self.options.changelist_view(self.fake_request, {})
138-
self.assertEquals(response.status_code, 200)
139-

tests/test_admin.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
from django.contrib import admin
2+
from django.contrib.auth.models import User
3+
from django.test import TestCase, RequestFactory
4+
5+
from constance.admin import Config
6+
7+
8+
class TestAdmin(TestCase):
9+
model = Config
10+
11+
def setUp(self):
12+
self.rf = RequestFactory()
13+
self.user = User.objects.create_superuser('admin', 'nimda', '[email protected]')
14+
self.options = admin.site._registry[self.model]
15+
self.client.login(username=self.user, password='nimda')
16+
17+
def test_changelist(self):
18+
request = self.rf.get('/admin/constance/config/')
19+
response = self.options.changelist_view(request, {})
20+
self.assertEquals(response.status_code, 200)

tests/test_database.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import sys
2+
3+
from django.test import TestCase
4+
5+
from constance.config import Config
6+
from constance import settings
7+
8+
from .storage import TestStorage
9+
10+
11+
class TestDatabase(TestCase, TestStorage):
12+
13+
def setUp(self):
14+
self.old_backend = settings.BACKEND
15+
settings.BACKEND = 'constance.backends.database.DatabaseBackend'
16+
17+
def tearDown(self):
18+
del sys.modules['constance']
19+
settings.BACKEND = self.old_backend
20+
import constance
21+
constance.config = Config()

tests/test_redis.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import sys
2+
3+
from django.test import TestCase
4+
5+
from constance.config import Config
6+
from constance import settings
7+
8+
from .storage import TestStorage
9+
10+
11+
class TestRedis(TestCase, TestStorage):
12+
13+
def setUp(self):
14+
self.old_backend = settings.BACKEND
15+
settings.BACKEND = 'constance.backends.redisd.RedisBackend'
16+
del sys.modules['constance']
17+
from constance import config
18+
config._backend._rd.clear()
19+
20+
def tearDown(self):
21+
del sys.modules['constance']
22+
from constance import config
23+
config._backend._rd.clear()
24+
settings.BACKEND = self.old_backend
25+
import constance
26+
constance.config = Config()

tests/testproject/__init__.py

Whitespace-only changes.

tests/testproject/models.py

Whitespace-only changes.

tests/testproject/test_app/__init__.py

Whitespace-only changes.

tests/testproject/test_app/models.py

Whitespace-only changes.

tests/testproject/test_app/tests/__init__.py

Lines changed: 0 additions & 1 deletion
This file was deleted.

tests/testproject/test_app/tests/helpers.py

Lines changed: 0 additions & 41 deletions
This file was deleted.

tests/testproject/urls.py

Lines changed: 0 additions & 12 deletions
This file was deleted.

tests/urls.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
from django.contrib import admin
2+
from django.conf.urls.defaults import patterns, include
3+
4+
urlpatterns = patterns('',
5+
(r'^admin/', include(admin.site.urls)),
6+
)

0 commit comments

Comments
 (0)