From 204225dd164cf3bfcd8bbc926c1cddc63d11d73a Mon Sep 17 00:00:00 2001 From: Amirhossein Khalili Date: Thu, 30 Apr 2026 12:41:38 +0330 Subject: [PATCH] test(backend): switch to django test runner --- .coveragerc | 11 ++++++ apps/notifications/tests/__init__.py | 1 + apps/time_entries/tests/__init__.py | 1 + apps/users/tests/__init__.py | 1 + config/settings/test.py | 1 + config/test_runner.py | 51 ++++++++++++++++++++++++++++ pytest.ini | 3 -- requirements/dev.txt | 9 +++-- 8 files changed, 70 insertions(+), 8 deletions(-) create mode 100644 .coveragerc create mode 100644 apps/notifications/tests/__init__.py create mode 100644 apps/time_entries/tests/__init__.py create mode 100644 apps/users/tests/__init__.py create mode 100644 config/test_runner.py delete mode 100644 pytest.ini diff --git a/.coveragerc b/.coveragerc new file mode 100644 index 0000000..3511f35 --- /dev/null +++ b/.coveragerc @@ -0,0 +1,11 @@ +[run] +branch = True +source = + apps +omit = + */migrations/* + */tests/* + +[report] +show_missing = True +skip_covered = False diff --git a/apps/notifications/tests/__init__.py b/apps/notifications/tests/__init__.py new file mode 100644 index 0000000..8b13789 --- /dev/null +++ b/apps/notifications/tests/__init__.py @@ -0,0 +1 @@ + diff --git a/apps/time_entries/tests/__init__.py b/apps/time_entries/tests/__init__.py new file mode 100644 index 0000000..8b13789 --- /dev/null +++ b/apps/time_entries/tests/__init__.py @@ -0,0 +1 @@ + diff --git a/apps/users/tests/__init__.py b/apps/users/tests/__init__.py new file mode 100644 index 0000000..8b13789 --- /dev/null +++ b/apps/users/tests/__init__.py @@ -0,0 +1 @@ + diff --git a/config/settings/test.py b/config/settings/test.py index 4503056..2855a04 100644 --- a/config/settings/test.py +++ b/config/settings/test.py @@ -28,3 +28,4 @@ CELERY_TASK_ALWAYS_EAGER = True CELERY_TASK_EAGER_PROPAGATES = True EMAIL_BACKEND = "django.core.mail.backends.locmem.EmailBackend" +TEST_RUNNER = "config.test_runner.AppDiscoverRunner" diff --git a/config/test_runner.py b/config/test_runner.py new file mode 100644 index 0000000..c18f279 --- /dev/null +++ b/config/test_runner.py @@ -0,0 +1,51 @@ +from pathlib import Path + +from django.apps import apps +from django.test.runner import DiscoverRunner + + +class AppDiscoverRunner(DiscoverRunner): + def load_tests_for_label(self, label, discover_kwargs): + if label.startswith("apps."): + try: + app_config = apps.get_app_config(label.split(".", 1)[1]) + except LookupError: + return super().load_tests_for_label(label, discover_kwargs) + + test_dir = Path(app_config.path) / "tests" + if test_dir.exists(): + repo_root = Path(__file__).resolve().parent.parent + discover_kwargs = dict(discover_kwargs) + discover_kwargs["top_level_dir"] = str(repo_root) + return self.test_loader.discover( + start_dir=str(test_dir), + **discover_kwargs, + ) + + return super().load_tests_for_label(label, discover_kwargs) + + def build_suite(self, test_labels=None, extra_tests=None, **kwargs): + if test_labels: + return super().build_suite( + test_labels, + extra_tests=extra_tests, + **kwargs, + ) + + suite = self.test_suite() + repo_root = Path(__file__).resolve().parent.parent + + for app_config in apps.get_app_configs(): + test_dir = Path(app_config.path) / "tests" + if app_config.name.startswith("apps.") and test_dir.exists(): + suite.addTests( + self.test_loader.discover( + start_dir=str(test_dir), + pattern=self.pattern, + top_level_dir=str(repo_root), + ) + ) + + if extra_tests: + suite.addTests(extra_tests) + return suite diff --git a/pytest.ini b/pytest.ini deleted file mode 100644 index e203b62..0000000 --- a/pytest.ini +++ /dev/null @@ -1,3 +0,0 @@ -[pytest] -DJANGO_SETTINGS_MODULE = config.settings.test -python_files = tests.py test_*.py *_tests.py diff --git a/requirements/dev.txt b/requirements/dev.txt index b422df7..88ed24a 100644 --- a/requirements/dev.txt +++ b/requirements/dev.txt @@ -7,10 +7,9 @@ ipython>=8.25 # Debug toolbar django-debug-toolbar>=4.4 -# Testing -pytest>=8.2 -pytest-django>=4.8 -factory-boy>=3.3 +# Testing +coverage>=7.10 +factory-boy>=3.3 # Linting & formatting black>=24.4 @@ -22,4 +21,4 @@ django-stubs>=5.0 # Pre-commit hooks pre-commit>=3.7 -commitizen>=4.13 \ No newline at end of file +commitizen>=4.13