root: allow usage of --randomly-seed for testing

Signed-off-by: Jens Langhammer <jens.langhammer@beryju.org>
This commit is contained in:
Jens Langhammer 2021-12-08 21:16:05 +01:00
parent 68a0684569
commit 402ed9bd20
1 changed files with 22 additions and 11 deletions

View File

@ -1,4 +1,6 @@
"""Integrate ./manage.py test with pytest"""
from argparse import ArgumentParser
from django.conf import settings
from authentik.lib.config import CONFIG
@ -8,10 +10,20 @@ from tests.e2e.utils import get_docker_tag
class PytestTestRunner: # pragma: no cover
"""Runs pytest to discover and run tests."""
def __init__(self, verbosity=1, failfast=False, keepdb=False, **_):
def __init__(self, verbosity=1, failfast=False, keepdb=False, **kwargs):
self.verbosity = verbosity
self.failfast = failfast
self.keepdb = keepdb
self.args = ["-vv"]
if self.failfast:
self.args.append("--exitfirst")
if self.keepdb:
self.args.append("--reuse-db")
if kwargs.get("randomly_seed", None):
self.args.append(f"--randomly-seed={kwargs['randomly_seed']}")
settings.TEST = True
settings.CELERY_TASK_ALWAYS_EAGER = True
CONFIG.y_set("authentik.avatars", "none")
@ -21,21 +33,20 @@ class PytestTestRunner: # pragma: no cover
f"goauthentik.io/dev-%(type)s:{get_docker_tag()}",
)
@classmethod
def add_arguments(cls, parser: ArgumentParser):
"""Add more pytest-specific arguments"""
parser.add_argument("--randomly-seed", type=int)
def run_tests(self, test_labels):
"""Run pytest and return the exitcode.
It translates some of Django's test command option to pytest's.
"""
import pytest
argv = ["-vv"]
if self.failfast:
argv.append("--exitfirst")
if self.keepdb:
argv.append("--reuse-db")
if any("tests/e2e" in label for label in test_labels):
argv.append("-pno:randomly")
argv.extend(test_labels)
return pytest.main(argv)
self.args.append("-pno:randomly")
self.args.extend(test_labels)
return pytest.main(self.args)