From e144e4336ffc26eda21aa3133df9e179982cdf9c Mon Sep 17 00:00:00 2001 From: Jannis Leidel Date: Fri, 22 Feb 2013 14:58:41 +0100 Subject: [PATCH] Refactored tests to use django-discover-runner and not an own testproject anymore. --- setup.py | 2 +- .../test_app => }/redis_mockup.py | 0 tests/runtests.py | 18 ++---- tests/{testproject => }/settings.py | 17 ++++-- .../tests/test_config.py => storage.py} | 58 +------------------ tests/test_admin.py | 20 +++++++ tests/test_database.py | 21 +++++++ tests/test_redis.py | 26 +++++++++ tests/testproject/__init__.py | 0 tests/testproject/models.py | 0 tests/testproject/test_app/__init__.py | 0 tests/testproject/test_app/models.py | 0 tests/testproject/test_app/tests/__init__.py | 1 - tests/testproject/test_app/tests/helpers.py | 41 ------------- tests/testproject/urls.py | 12 ---- tests/urls.py | 6 ++ 16 files changed, 92 insertions(+), 130 deletions(-) rename tests/{testproject/test_app => }/redis_mockup.py (100%) rename tests/{testproject => }/settings.py (73%) rename tests/{testproject/test_app/tests/test_config.py => storage.py} (67%) create mode 100644 tests/test_admin.py create mode 100644 tests/test_database.py create mode 100644 tests/test_redis.py delete mode 100644 tests/testproject/__init__.py delete mode 100644 tests/testproject/models.py delete mode 100644 tests/testproject/test_app/__init__.py delete mode 100644 tests/testproject/test_app/models.py delete mode 100644 tests/testproject/test_app/tests/__init__.py delete mode 100644 tests/testproject/test_app/tests/helpers.py delete mode 100644 tests/testproject/urls.py create mode 100644 tests/urls.py diff --git a/setup.py b/setup.py index 1ba0ded..06f3dfe 100644 --- a/setup.py +++ b/setup.py @@ -34,5 +34,5 @@ setup( packages=find_packages(exclude=['tests']), include_package_data=True, test_suite='tests.runtests.main', + test_requires=['django-discover-runner'], ) - diff --git a/tests/testproject/test_app/redis_mockup.py b/tests/redis_mockup.py similarity index 100% rename from tests/testproject/test_app/redis_mockup.py rename to tests/redis_mockup.py diff --git a/tests/runtests.py b/tests/runtests.py index 2d259c6..ab3e18b 100755 --- a/tests/runtests.py +++ b/tests/runtests.py @@ -1,23 +1,15 @@ #!/usr/bin/env python - -"""Borrowed from Carl Meyer's django-adminfiles.""" - import os import sys +from django.core.management import call_command -testapp_dir = os.path.dirname(os.path.abspath(__file__)) -constance_dir = os.path.dirname(testapp_dir) -sys.path.insert(0, constance_dir) -sys.path.insert(0, testapp_dir) +os.environ['DJANGO_SETTINGS_MODULE'] = 'tests.settings' -os.environ['DJANGO_SETTINGS_MODULE'] = 'testproject.settings' - -from django.test.simple import DjangoTestSuiteRunner def main(): - runner = DjangoTestSuiteRunner() - failures = runner.run_tests(['test_app'], verbosity=1, interactive=True) - sys.exit(failures) + result = call_command('test', 'tests', verbosity=2) + sys.exit(result) + if __name__ == '__main__': main() diff --git a/tests/testproject/settings.py b/tests/settings.py similarity index 73% rename from tests/testproject/settings.py rename to tests/settings.py index 9080f2e..f796cdc 100644 --- a/tests/testproject/settings.py +++ b/tests/settings.py @@ -1,13 +1,21 @@ # -*- encoding: utf-8 -*- - +import os from datetime import datetime, date, time from decimal import Decimal +# using the parent directory as the base for the test discovery +TEST_DISCOVER_TOP_LEVEL = os.path.join(os.path.dirname(__file__), '..') + +TEST_RUNNER = 'discover_runner.DiscoverRunner' + SECRET_KEY = 'cheese' +DATABASE_ENGINE = 'sqlite3' + DATABASES = { 'default': { 'ENGINE': 'django.db.backends.sqlite3', + 'NAME': ':memory:', } } @@ -16,16 +24,15 @@ INSTALLED_APPS = ( 'django.contrib.auth', 'django.contrib.contenttypes', 'django.contrib.admin', + 'south', 'constance', 'constance.backends.database', - - 'testproject.test_app', ) -ROOT_URLCONF = 'testproject.urls' +ROOT_URLCONF = 'tests.urls' -CONSTANCE_CONNECTION_CLASS = 'testproject.test_app.redis_mockup.Connection' +CONSTANCE_CONNECTION_CLASS = 'tests.redis_mockup.Connection' CONSTANCE_CONFIG = { 'INT_VALUE': (1, 'some int'), diff --git a/tests/testproject/test_app/tests/test_config.py b/tests/storage.py similarity index 67% rename from tests/testproject/test_app/tests/test_config.py rename to tests/storage.py index b667871..89d8fa3 100644 --- a/tests/testproject/test_app/tests/test_config.py +++ b/tests/storage.py @@ -1,20 +1,8 @@ # -*- encoding: utf-8 -*- - import sys from datetime import datetime, date, time from decimal import Decimal -from django.test import TestCase -from django.conf import settings -from django.contrib import admin -from django.contrib.auth.models import User - -from constance import settings -from constance.admin import Config - -# Use django RequestFactory later on -from testproject.test_app.tests.helpers import FakeRequest - class TestStorage(object): @@ -71,24 +59,7 @@ class TestStorage(object): pass self.assertEquals(type(e), AttributeError) -class TestRedis(TestCase, TestStorage): - - def setUp(self): - self.old_backend = settings.BACKEND - settings.BACKEND = 'constance.backends.redisd.RedisBackend' - del sys.modules['constance'] - from constance import config - config._backend._rd.clear() - - def tearDown(self): - del sys.modules['constance'] - from constance import config - config._backend._rd.clear() - settings.BACKEND = self.old_backend - import constance - constance.config = Config() - - def testMissingValues(self): + def test_missing_values(self): from constance import config # set some values and leave out others @@ -110,30 +81,3 @@ class TestRedis(TestCase, TestStorage): self.assertEquals(config.FLOAT_VALUE, 3.1415926536) # this should be the default value self.assertEquals(config.DATE_VALUE, date(2001, 12, 20)) self.assertEquals(config.TIME_VALUE, time(1, 59, 0)) - - -class TestDatabase(TestCase, TestStorage): - - def setUp(self): - self.old_backend = settings.BACKEND - settings.BACKEND = 'constance.backends.database.DatabaseBackend' - - def tearDown(self): - del sys.modules['constance'] - settings.BACKEND = self.old_backend - import constance - constance.config = Config() - -class TestAdmin(TestCase): - model = Config - - def setUp(self): - self.user = User.objects.create_superuser('admin', 'nimda', 'a@a.cz') - self.options = admin.site._registry[self.model] - self.fake_request = FakeRequest(user=self.user) - self.client.login(username=self.user, password='nimda') - - def test_changelist(self): - response = self.options.changelist_view(self.fake_request, {}) - self.assertEquals(response.status_code, 200) - diff --git a/tests/test_admin.py b/tests/test_admin.py new file mode 100644 index 0000000..e665f62 --- /dev/null +++ b/tests/test_admin.py @@ -0,0 +1,20 @@ +from django.contrib import admin +from django.contrib.auth.models import User +from django.test import TestCase, RequestFactory + +from constance.admin import Config + + +class TestAdmin(TestCase): + model = Config + + def setUp(self): + self.rf = RequestFactory() + self.user = User.objects.create_superuser('admin', 'nimda', 'a@a.cz') + self.options = admin.site._registry[self.model] + self.client.login(username=self.user, password='nimda') + + def test_changelist(self): + request = self.rf.get('/admin/constance/config/') + response = self.options.changelist_view(request, {}) + self.assertEquals(response.status_code, 200) diff --git a/tests/test_database.py b/tests/test_database.py new file mode 100644 index 0000000..d861b86 --- /dev/null +++ b/tests/test_database.py @@ -0,0 +1,21 @@ +import sys + +from django.test import TestCase + +from constance.config import Config +from constance import settings + +from .storage import TestStorage + + +class TestDatabase(TestCase, TestStorage): + + def setUp(self): + self.old_backend = settings.BACKEND + settings.BACKEND = 'constance.backends.database.DatabaseBackend' + + def tearDown(self): + del sys.modules['constance'] + settings.BACKEND = self.old_backend + import constance + constance.config = Config() diff --git a/tests/test_redis.py b/tests/test_redis.py new file mode 100644 index 0000000..381af95 --- /dev/null +++ b/tests/test_redis.py @@ -0,0 +1,26 @@ +import sys + +from django.test import TestCase + +from constance.config import Config +from constance import settings + +from .storage import TestStorage + + +class TestRedis(TestCase, TestStorage): + + def setUp(self): + self.old_backend = settings.BACKEND + settings.BACKEND = 'constance.backends.redisd.RedisBackend' + del sys.modules['constance'] + from constance import config + config._backend._rd.clear() + + def tearDown(self): + del sys.modules['constance'] + from constance import config + config._backend._rd.clear() + settings.BACKEND = self.old_backend + import constance + constance.config = Config() diff --git a/tests/testproject/__init__.py b/tests/testproject/__init__.py deleted file mode 100644 index e69de29..0000000 diff --git a/tests/testproject/models.py b/tests/testproject/models.py deleted file mode 100644 index e69de29..0000000 diff --git a/tests/testproject/test_app/__init__.py b/tests/testproject/test_app/__init__.py deleted file mode 100644 index e69de29..0000000 diff --git a/tests/testproject/test_app/models.py b/tests/testproject/test_app/models.py deleted file mode 100644 index e69de29..0000000 diff --git a/tests/testproject/test_app/tests/__init__.py b/tests/testproject/test_app/tests/__init__.py deleted file mode 100644 index 389b072..0000000 --- a/tests/testproject/test_app/tests/__init__.py +++ /dev/null @@ -1 +0,0 @@ -from testproject.test_app.tests.test_config import * diff --git a/tests/testproject/test_app/tests/helpers.py b/tests/testproject/test_app/tests/helpers.py deleted file mode 100644 index c85c769..0000000 --- a/tests/testproject/test_app/tests/helpers.py +++ /dev/null @@ -1,41 +0,0 @@ -import datetime - -from django.core.handlers.wsgi import WSGIRequest - - - -class FakeRequest(WSGIRequest): - - def __init__(self, user=None, meta=None, environ=None, cookies=None): - if not environ: - environ = { - 'PATH_INFO': '/', - 'QUERY_STRING': '', - 'REMOTE_ADDR': '127.0.0.1', - 'REQUEST_METHOD': 'GET', - 'SCRIPT_NAME': '', - 'SERVER_NAME': 'testserver', - 'SERVER_PORT': '80', - 'SERVER_PROTOCOL': 'HTTP/1.1', - 'wsgi.version': (1,0), - 'wsgi.url_scheme': 'http', - 'wsgi.errors': [], - 'wsgi.multiprocess': True, - 'wsgi.multithread': False, - 'wsgi.run_once': False, - 'wsgi.input': None, - } - super(FakeRequest, self).__init__(environ) - if user: - self.user = user - if meta: - self.META.update(meta) - if cookies: - self.COOKIES.update(cookies) - self.xnow = datetime.datetime.now() - - @classmethod - def from_test_response(cls, response, *args, **kwargs): - return cls(environ=response.request, *args, **kwargs) - - diff --git a/tests/testproject/urls.py b/tests/testproject/urls.py deleted file mode 100644 index 6459b42..0000000 --- a/tests/testproject/urls.py +++ /dev/null @@ -1,12 +0,0 @@ -from django.contrib import admin - -from django.conf.urls.defaults import patterns, include - -urlpatterns = patterns('', - # Uncomment the admin/doc line below and add 'django.contrib.admindocs' - # to INSTALLED_APPS to enable admin documentation: - # (r'^admin/doc/', include('django.contrib.admindocs.urls')), - - (r'^admin/', include(admin.site.urls)), -) - diff --git a/tests/urls.py b/tests/urls.py new file mode 100644 index 0000000..8711375 --- /dev/null +++ b/tests/urls.py @@ -0,0 +1,6 @@ +from django.contrib import admin +from django.conf.urls.defaults import patterns, include + +urlpatterns = patterns('', + (r'^admin/', include(admin.site.urls)), +)