From 402ed9bd200fbe05d826614141014324d9568a1c Mon Sep 17 00:00:00 2001 From: Jens Langhammer Date: Wed, 8 Dec 2021 21:16:05 +0100 Subject: [PATCH] root: allow usage of --randomly-seed for testing Signed-off-by: Jens Langhammer --- authentik/root/test_runner.py | 33 ++++++++++++++++++++++----------- 1 file changed, 22 insertions(+), 11 deletions(-) diff --git a/authentik/root/test_runner.py b/authentik/root/test_runner.py index 8af38df32..2a6d498b0 100644 --- a/authentik/root/test_runner.py +++ b/authentik/root/test_runner.py @@ -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)