2012-08-08 06:32:28 +02:00
|
|
|
import os
|
|
|
|
import sys
|
|
|
|
import argparse
|
2015-01-14 14:10:09 +01:00
|
|
|
|
|
|
|
import django
|
2012-08-08 06:32:28 +02:00
|
|
|
from django.conf import settings
|
|
|
|
|
2015-01-14 14:10:09 +01:00
|
|
|
|
2012-08-08 06:32:28 +02:00
|
|
|
class QuickDjangoTest(object):
|
|
|
|
"""
|
|
|
|
A quick way to run the Django test suite without a fully-configured project.
|
|
|
|
|
|
|
|
Example usage:
|
|
|
|
|
|
|
|
>>> QuickDjangoTest('app1', 'app2')
|
|
|
|
|
2014-07-21 10:21:33 +02:00
|
|
|
Based on a script published by Lukasz Dziedzia at:
|
2012-08-08 06:32:28 +02:00
|
|
|
http://stackoverflow.com/questions/3841725/how-to-launch-tests-for-django-reusable-app
|
|
|
|
"""
|
|
|
|
DIRNAME = os.path.dirname(__file__)
|
|
|
|
INSTALLED_APPS = (
|
2015-11-16 16:10:58 +01:00
|
|
|
'django.contrib.admin',
|
2012-08-08 06:32:28 +02:00
|
|
|
'django.contrib.auth',
|
|
|
|
'django.contrib.contenttypes',
|
2015-11-16 16:10:58 +01:00
|
|
|
'django.contrib.humanize',
|
|
|
|
'django.contrib.messages',
|
2012-08-08 06:32:28 +02:00
|
|
|
'django.contrib.sessions',
|
2015-11-16 16:10:58 +01:00
|
|
|
'django.contrib.sites',
|
2012-08-08 06:32:28 +02:00
|
|
|
'django.contrib.staticfiles',
|
2014-06-05 01:42:58 +02:00
|
|
|
'bootstrapform',
|
2012-08-08 06:32:28 +02:00
|
|
|
)
|
2017-12-28 13:23:51 +01:00
|
|
|
MIDDLEWARE = [
|
|
|
|
'django.middleware.security.SecurityMiddleware',
|
2015-01-14 15:54:39 +01:00
|
|
|
'django.contrib.sessions.middleware.SessionMiddleware',
|
2015-01-14 15:49:08 +01:00
|
|
|
'django.middleware.common.CommonMiddleware',
|
|
|
|
'django.middleware.csrf.CsrfViewMiddleware',
|
|
|
|
'django.contrib.auth.middleware.AuthenticationMiddleware',
|
|
|
|
'django.contrib.messages.middleware.MessageMiddleware',
|
2015-01-14 15:54:39 +01:00
|
|
|
'django.middleware.clickjacking.XFrameOptionsMiddleware',
|
2015-01-14 15:49:08 +01:00
|
|
|
]
|
2012-08-08 06:32:28 +02:00
|
|
|
|
2016-10-20 08:26:06 +02:00
|
|
|
TEMPLATES = [
|
|
|
|
{
|
|
|
|
'BACKEND': 'django.template.backends.django.DjangoTemplates',
|
|
|
|
'APP_DIRS': True,
|
|
|
|
'OPTIONS': {
|
|
|
|
'context_processors': (
|
|
|
|
# Defaults:
|
|
|
|
"django.contrib.auth.context_processors.auth",
|
|
|
|
"django.template.context_processors.debug",
|
|
|
|
"django.template.context_processors.i18n",
|
|
|
|
"django.template.context_processors.media",
|
|
|
|
"django.template.context_processors.static",
|
|
|
|
"django.template.context_processors.tz",
|
|
|
|
"django.contrib.messages.context_processors.messages",
|
|
|
|
# Our extra:
|
|
|
|
"django.template.context_processors.request",
|
|
|
|
),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
]
|
|
|
|
|
2012-08-08 06:32:28 +02:00
|
|
|
def __init__(self, *args, **kwargs):
|
|
|
|
self.apps = args
|
2016-11-21 01:32:56 +01:00
|
|
|
self._tests()
|
2012-08-08 06:32:28 +02:00
|
|
|
|
2016-11-21 01:32:56 +01:00
|
|
|
def _tests(self):
|
2015-01-14 15:36:37 +01:00
|
|
|
|
2012-08-08 06:32:28 +02:00
|
|
|
settings.configure(
|
2016-10-24 08:04:31 +02:00
|
|
|
DEBUG=True,
|
2020-04-15 11:18:37 +02:00
|
|
|
TIME_ZONE='UTC',
|
2016-10-24 08:04:31 +02:00
|
|
|
DATABASES={
|
2012-08-08 06:32:28 +02:00
|
|
|
'default': {
|
|
|
|
'ENGINE': 'django.db.backends.sqlite3',
|
|
|
|
'NAME': os.path.join(self.DIRNAME, 'database.db'),
|
|
|
|
'USER': '',
|
|
|
|
'PASSWORD': '',
|
|
|
|
'HOST': '',
|
|
|
|
'PORT': '',
|
|
|
|
}
|
|
|
|
},
|
2016-10-24 08:04:31 +02:00
|
|
|
INSTALLED_APPS=self.INSTALLED_APPS + self.apps,
|
2017-12-28 13:23:51 +01:00
|
|
|
MIDDLEWARE=self.MIDDLEWARE,
|
2016-10-24 08:04:31 +02:00
|
|
|
ROOT_URLCONF='helpdesk.tests.urls',
|
|
|
|
STATIC_URL='/static/',
|
|
|
|
TEMPLATES=self.TEMPLATES
|
2012-08-08 06:32:28 +02:00
|
|
|
)
|
2015-01-14 15:36:37 +01:00
|
|
|
|
2016-11-21 01:32:56 +01:00
|
|
|
from django.test.runner import DiscoverRunner
|
|
|
|
test_runner = DiscoverRunner(verbosity=1)
|
|
|
|
django.setup()
|
2015-01-14 14:10:09 +01:00
|
|
|
|
2015-06-12 15:30:54 +02:00
|
|
|
failures = test_runner.run_tests(self.apps)
|
2012-08-08 06:32:28 +02:00
|
|
|
if failures:
|
|
|
|
sys.exit(failures)
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
"""
|
|
|
|
What do when the user hits this file from the shell.
|
|
|
|
|
|
|
|
Example usage:
|
|
|
|
|
|
|
|
$ python quicktest.py app1 app2
|
|
|
|
|
|
|
|
"""
|
|
|
|
parser = argparse.ArgumentParser(
|
|
|
|
usage="[args]",
|
|
|
|
description="Run Django tests on the provided applications."
|
|
|
|
)
|
|
|
|
parser.add_argument('apps', nargs='+', type=str)
|
|
|
|
args = parser.parse_args()
|
|
|
|
QuickDjangoTest(*args.apps)
|