Tests: Simplify Django version compatibility

* Restructure runtests.py as suggested in
  https://docs.djangoproject.com/en/1.9/topics/testing/advanced/#using-the-django-test-runner-to-test-reusable-applications

* Load version-specific Django settings modules
  (rather than trying to make a single settings.configure()
  work with all supported versions).

* Use `django-admin startproject` defaults for all
  settings modules. (Tests compatibility with typical
  apps, middleware, and other settings.)

* Set up tests-specific url config; switch to literal
  urls in test cases. (Eliminates url `reverse` from
  tests.)

* Make runtests.py executable

Closes #18
This commit is contained in:
medmunds
2016-05-31 11:01:45 -07:00
parent 296f6cab50
commit 34af81aee6
10 changed files with 435 additions and 106 deletions

68
runtests.py Normal file → Executable file
View File

@@ -1,57 +1,33 @@
#!/usr/bin/env python
# python setup.py test
# or
# python runtests.py [anymail.tests.test_x anymail.tests.test_y.SomeTestCase ...]
# runtests.py [tests.test_x tests.test_y.SomeTestCase ...]
import sys
import django
import os
import warnings
from django import setup
from django.conf import settings
from django.test.runner import DiscoverRunner as TestRunner
warnings.simplefilter('default') # show DeprecationWarning and other default-ignored warnings
APP = 'anymail'
settings.configure(
DEBUG=True,
DATABASES={
'default': {
'ENGINE': 'django.db.backends.sqlite3',
}
},
ROOT_URLCONF=APP+'.urls',
INSTALLED_APPS=(
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.admin',
APP,
),
MIDDLEWARE_CLASSES=(
'django.middleware.common.CommonMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
),
TEMPLATES=[
# Anymail doesn't have any templates, but tests need a TEMPLATES
# setting to avoid warnings from the Django 1.8+ test client.
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
},
],
)
# Initialize Django app registry
setup()
from django.test.utils import get_runner
def runtests(*args):
def runtests(test_labels=None):
"""Discover and run project tests. Returns number of failures."""
test_labels = test_labels or ['tests']
# noinspection PyStringFormat
os.environ['DJANGO_SETTINGS_MODULE'] = \
'tests.test_settings.settings_%d_%d' % django.VERSION[:2]
django.setup()
TestRunner = get_runner(settings)
test_runner = TestRunner(verbosity=1)
test_labels = args if len(args) > 0 else ['tests']
failures = test_runner.run_tests(test_labels)
sys.exit(failures)
return test_runner.run_tests(test_labels)
if __name__ == '__main__':
runtests(*sys.argv[1:])
warnings.simplefilter('default') # show DeprecationWarning and other default-ignored warnings
failures = runtests(test_labels=sys.argv[1:])
sys.exit(bool(failures))