4fd6a9d8d75cba574e3ffe7618500e563de9df58
[django-ssify.git] / runtests.py
1 #!/usr/bin/env python
2 # -*- coding: utf-8
3 # This file is part of django-ssify, licensed under GNU Affero GPLv3 or later.
4 # Copyright © Fundacja Nowoczesna Polska. See README.md for more information.
5 #
6 """
7 Creates a simple Django configuration and runs tests for django-ssify.
8 """
9 from __future__ import unicode_literals
10 import sys
11 import os
12 from os.path import dirname, abspath
13 from optparse import OptionParser
14
15 from django.conf import settings
16
17 # For convenience configure settings if they are not pre-configured or if we
18 # haven't been provided settings to use by environment variable.
19 if not settings.configured and not os.environ.get('DJANGO_SETTINGS_MODULE'):
20     settings.configure(
21         CACHES={
22             'default': {'BACKEND':
23                         'django.core.cache.backends.locmem.LocMemCache'},
24             'ssify': {'BACKEND':
25                       'django.core.cache.backends.locmem.LocMemCache'},
26         },
27         DATABASES={
28             'default': {
29                 'ENGINE': 'django.db.backends.sqlite3',
30             }
31         },
32         INSTALLED_APPS=[
33             'django.contrib.auth',
34             'django.contrib.contenttypes',
35             'django.contrib.sessions',
36             'django.contrib.sites',
37
38             'ssify',
39             'tests',
40         ],
41         LANGUAGE_CODE='pl',
42         MEDIA_URL='/media/',
43         MIDDLEWARE_CLASSES=[
44             'django.middleware.csrf.CsrfViewMiddleware',
45             'ssify.middleware.SsiMiddleware',
46             'django.middleware.cache.UpdateCacheMiddleware',
47             'ssify.middleware.PrepareForCacheMiddleware',
48             'django.middleware.common.CommonMiddleware',
49             'django.contrib.sessions.middleware.SessionMiddleware',
50             'ssify.middleware.LocaleMiddleware',
51             'django.middleware.cache.FetchFromCacheMiddleware',
52         ],
53         STATIC_URL='/static/',
54         ROOT_URLCONF='tests.urls',
55         SITE_ID=1,
56         TEMPLATE_CONTEXT_PROCESSORS=(
57             "django.core.context_processors.debug",
58             "django.core.context_processors.i18n",
59             "django.core.context_processors.tz",
60             "django.core.context_processors.request",
61         ),
62     )
63
64 try:
65     from django.test.runner import DiscoverRunner
66 except ImportError:
67     # Django < 1.6
68     from django.test.simple import DjangoTestSuiteRunner as DiscoverRunner
69
70
71 def runtests(*test_args, **kwargs):
72     """Actual test suite entry point."""
73     if not test_args:
74         test_args = ['tests']
75     parent = dirname(abspath(__file__))
76     sys.path.insert(0, parent)
77
78     # For Django 1.7+
79     try:
80         from django import setup
81     except ImportError:
82         pass
83     else:
84         setup()
85
86     test_runner = DiscoverRunner(
87         verbosity=kwargs.get('verbosity', 1),
88         interactive=kwargs.get('interactive', False),
89         failfast=kwargs.get('failfast'))
90     failures = test_runner.run_tests(test_args)
91     sys.exit(failures)
92
93 if __name__ == '__main__':
94     parser = OptionParser()
95     parser.add_option('--failfast', action='store_true',
96                       default=False, dest='failfast')
97
98     (options, args) = parser.parse_args()
99
100     runtests(failfast=options.failfast, *args)