test(backend): switch to django test runner

This commit is contained in:
2026-04-30 12:41:38 +03:30
parent a2de2a133c
commit 204225dd16
8 changed files with 70 additions and 8 deletions

11
.coveragerc Normal file
View File

@@ -0,0 +1,11 @@
[run]
branch = True
source =
apps
omit =
*/migrations/*
*/tests/*
[report]
show_missing = True
skip_covered = False

View File

@@ -0,0 +1 @@

View File

@@ -0,0 +1 @@

View File

@@ -0,0 +1 @@

View File

@@ -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"

51
config/test_runner.py Normal file
View File

@@ -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

View File

@@ -1,3 +0,0 @@
[pytest]
DJANGO_SETTINGS_MODULE = config.settings.test
python_files = tests.py test_*.py *_tests.py

View File

@@ -8,8 +8,7 @@ ipython>=8.25
django-debug-toolbar>=4.4
# Testing
pytest>=8.2
pytest-django>=4.8
coverage>=7.10
factory-boy>=3.3
# Linting & formatting