From 81a096426d1b80713d044a1aa00033dd283490fa Mon Sep 17 00:00:00 2001 From: Mathieu Kniewallner Date: Mon, 8 Apr 2024 12:58:03 +0200 Subject: [PATCH] test(conftest): ensure that `pytest_sessionstart` hook runs once (#669) --- pyproject.toml | 4 ++++ tests/functional/conftest.py | 10 +++++++++- 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/pyproject.toml b/pyproject.toml index 984b7b85..14ab1c39 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -107,6 +107,10 @@ warn_unreachable = true pretty = true show_error_codes = true +[[tool.mypy.overrides]] +module = ["xdist.*"] +ignore_missing_imports = true + [tool.deptry] extend_exclude = [ "docs", diff --git a/tests/functional/conftest.py b/tests/functional/conftest.py index f9dfc0b7..160d8231 100644 --- a/tests/functional/conftest.py +++ b/tests/functional/conftest.py @@ -6,12 +6,20 @@ from pathlib import Path import pytest +import xdist from tests.functional.utils import DEPTRY_WHEEL_DIRECTORY from tests.utils import PDMVenvFactory, PipVenvFactory, PoetryVenvFactory -def pytest_sessionstart() -> None: +def pytest_sessionstart(session: pytest.Session) -> None: + # When running the tests on multiple workers with pytest-xdist, the hook will be run several times: + # - once from "master" node, when test suite starts + # - X times (where X is the number of workers), when tests start in each worker + # We only want to run the hook once, so we explicitly skip the hook if running on a pytest-xdist worker. + if xdist.is_xdist_worker(session): + return None + deptry_wheel_path = Path(DEPTRY_WHEEL_DIRECTORY) print(f"Building `deptry` wheel in {deptry_wheel_path} to use it on functional tests...") # noqa: T201