Initial
This commit is contained in:
71
tests/test_suite.py
Normal file
71
tests/test_suite.py
Normal file
@@ -0,0 +1,71 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
from __future__ import unicode_literals
|
||||
import datetime
|
||||
import django
|
||||
import os
|
||||
import shutil
|
||||
import sys
|
||||
from django.test.utils import get_runner
|
||||
|
||||
from base.console_scripts.admin import DJANGO_MAIN_MODULE
|
||||
from base.tests.utils import mkdtemp
|
||||
|
||||
|
||||
class DjangoEnvironment(object):
|
||||
@staticmethod
|
||||
def _install_djangoproject(path):
|
||||
cmd = 'django-test-admin setup "{}"'.format(path)
|
||||
os.system(cmd)
|
||||
|
||||
def __init__(self, path=None, remove_after=True):
|
||||
self.path = path
|
||||
|
||||
self._remove_after = remove_after
|
||||
self._original_sys_path = None
|
||||
self._modified_sys_path = None
|
||||
|
||||
def __enter__(self):
|
||||
if self.path is None:
|
||||
prefix = 'testrun-{datetime}-'.format(
|
||||
datetime=datetime.datetime.now().strftime('%Y%m%d-%H%M')
|
||||
)
|
||||
self.path = mkdtemp(prefix=prefix)
|
||||
|
||||
self._install_djangoproject(self.path)
|
||||
|
||||
self._original_sys_path = sys.path
|
||||
sys.path.append(self.path)
|
||||
self._modified_sys_path = sys.path
|
||||
|
||||
os.environ['DJANGO_SETTINGS_MODULE'] = '{}.settings'.format(DJANGO_MAIN_MODULE)
|
||||
django.setup()
|
||||
|
||||
from django.conf import settings
|
||||
self.settings = settings
|
||||
|
||||
return self
|
||||
|
||||
def __exit__(self, *args):
|
||||
if self._modified_sys_path is not None and self._original_sys_path is not None:
|
||||
if sys.path == self._modified_sys_path:
|
||||
sys.path = self._original_sys_path
|
||||
if self._remove_after:
|
||||
shutil.rmtree(self.path)
|
||||
|
||||
|
||||
class TestSuite(object):
|
||||
@staticmethod
|
||||
def run():
|
||||
tests = ['base']
|
||||
test_tags = None
|
||||
exclude_test_tags = None
|
||||
|
||||
with DjangoEnvironment() as env:
|
||||
test_runner_class = get_runner(env.settings)
|
||||
test_runner = test_runner_class(tags=test_tags, exclude_tags=exclude_test_tags)
|
||||
failures = test_runner.run_tests(tests)
|
||||
|
||||
return bool(failures)
|
||||
|
||||
def __call__(self):
|
||||
sys.exit(self.run())
|
||||
Reference in New Issue
Block a user