diff --git a/conftest.py b/conftest.py index 4b46b276a0..0e4d7eb5f4 100644 --- a/conftest.py +++ b/conftest.py @@ -5,6 +5,8 @@ import pytest import vcr from django.conf import settings +from pytest_django.fixtures import _set_suffix_to_test_databases +from pytest_django.lazy_django import skip_if_no_django from shared.reports.resources import Report, ReportFile, ReportLine from shared.utils.sessions import Session @@ -22,6 +24,17 @@ def pytest_configure(config): pass +@pytest.fixture(scope="session") +def django_db_modify_db_settings_xdist_suffix(request): + skip_if_no_django() + + xdist_suffix = getattr(request.config, "workerinput", {}).get("workerid") + if xdist_suffix: + # 'gw0' -> '1', 'gw1' -> '2', ... + suffix = str(int(xdist_suffix.replace("gw", "")) + 1) + _set_suffix_to_test_databases(suffix=suffix) + + @pytest.fixture def codecov_vcr(request): current_path = Path(request.node.fspath) @@ -48,7 +61,7 @@ def mock_redis(mocker): yield redis_server -@pytest.fixture(scope="class") +@pytest.fixture def sample_report(request): report = Report() first_file = ReportFile("foo/file1.py") diff --git a/pytest.ini b/pytest.ini index 1a1cea04cc..14c9f1c3ed 100644 --- a/pytest.ini +++ b/pytest.ini @@ -1,3 +1,3 @@ [pytest] DJANGO_SETTINGS_MODULE = codecov.settings_dev -addopts = -p no:warnings --ignore=shared --ignore-glob=**/test_results* +addopts = --reuse-db -n 3 --dist no -p no:warnings --ignore=shared --ignore-glob=**/test_results* \ No newline at end of file diff --git a/requirements.in b/requirements.in index fdd21e56b8..230cd35c89 100644 --- a/requirements.in +++ b/requirements.in @@ -37,6 +37,7 @@ pytest-cov pytest-django pytest-mock pytest-asyncio +pytest-xdist python-dateutil python-json-logger python-redis-lock diff --git a/requirements.txt b/requirements.txt index 61e5bcf67a..9130cacbad 100644 --- a/requirements.txt +++ b/requirements.txt @@ -162,6 +162,8 @@ elastic-apm==6.13.2 # via -r requirements.in envier==0.5.1 # via ddtrace +execnet==2.1.1 + # via pytest-xdist factory-boy==3.2.0 # via -r requirements.in faker==4.1.3 @@ -349,6 +351,7 @@ pytest==8.1.1 # pytest-cov # pytest-django # pytest-mock + # pytest-xdist pytest-asyncio==0.23.6 # via -r requirements.in pytest-cov==5.0.0 @@ -357,6 +360,8 @@ pytest-django==4.8.0 # via -r requirements.in pytest-mock==3.14.0 # via -r requirements.in +pytest-xdist==3.6.1 + # via -r requirements.in python-dateutil==2.9.0.post0 # via # -r requirements.in diff --git a/webhook_handlers/tests/test_github.py b/webhook_handlers/tests/test_github.py index d4a947002d..2fdc407adb 100644 --- a/webhook_handlers/tests/test_github.py +++ b/webhook_handlers/tests/test_github.py @@ -50,7 +50,7 @@ def __getitem__(self, key): class GithubWebhookHandlerTests(APITestCase): - @pytest.fixture(scope="function", autouse=True) + @pytest.fixture(autouse=True) def inject_mocker(request, mocker): request.mocker = mocker diff --git a/webhook_handlers/tests/test_gitlab_enterprise.py b/webhook_handlers/tests/test_gitlab_enterprise.py index 7d7737d7c5..8d8d133356 100644 --- a/webhook_handlers/tests/test_gitlab_enterprise.py +++ b/webhook_handlers/tests/test_gitlab_enterprise.py @@ -21,7 +21,7 @@ class TestGitlabEnterpriseWebhookHandler(APITestCase): - @pytest.fixture(scope="function", autouse=True) + @pytest.fixture(autouse=True) def inject_mocker(request, mocker): request.mocker = mocker