UPD: try to make useful test stuff.

This commit is contained in:
2019-03-15 17:35:16 +01:00
parent 2dce52cb25
commit 1c3e5bf761
6 changed files with 130 additions and 2 deletions

2
tests/__init__.py Normal file
View File

@@ -0,0 +1,2 @@
from test_suite import TestSuite
test_suite = TestSuite()

74
tests/test_suite.py Normal file
View File

@@ -0,0 +1,74 @@
import datetime
import django
import os
import shutil
import sys
from django.conf import settings
from django.test.utils import get_runner
from tempfile import mkdtemp
class DjangoEnvironment(object):
def __init__(self, path=None, base_path=None, remove_after=True,
enable_modules=None):
self.path = path
if base_path is not None:
self._base_path = base_path
else:
self._base_path = os.getcwd()
self._remove_after = remove_after
self._original_sys_path = None
self._modified_sys_path = None
if enable_modules is not None:
self._enable_modules = enable_modules
else:
self._enable_modules = []
def admin_cmd(self, *args):
cmd = 'django-dav-admin'
for arg in args:
cmd += ' "{}"'.format(arg)
os.system(cmd)
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, dir=self._base_path)
self.admin_cmd('setup', self.path)
for mod in self._enable_modules:
self.admin_cmd('enable_module', self.path, mod)
self._original_sys_path = sys.path
sys.path.append(self.path)
self._modified_sys_path = sys.path
os.environ['DJANGO_SETTINGS_MODULE'] = 'main.settings'
django.setup()
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):
def __call__(self):
modules = ['dav_auth']
tests = ['dav_base'] + modules
test_tags = None
exclude_test_tags = ['browser']
failures = 0
with DjangoEnvironment(enable_modules=modules):
test_runner_class = get_runner(settings)
test_runner = test_runner_class(tags=test_tags, exclude_tags=exclude_test_tags)
failures = test_runner.run_tests(tests)
sys.exit(bool(failures))