Coverage for conftest.py: 79%
39 statements
« prev ^ index » next coverage.py v7.6.10, created at 2025-01-21 01:50 +0000
« prev ^ index » next coverage.py v7.6.10, created at 2025-01-21 01:50 +0000
1#
2# Copyright (c) Microsoft Corporation.
3# Licensed under the MIT License.
4#
5"""Provides some pytest configuration overrides for both modules."""
7# Note: This file is named conftest.py so that pytest picks it up automatically
8# without the need to adjust PYTHONPATH or sys.path as much.
10import os
11import shutil
12from tempfile import mkdtemp
13from warnings import warn
15import pytest
16from xdist.workermanage import WorkerController
19def is_master(config: pytest.Config) -> bool:
20 """True if the code running the given pytest.config object is running in a xdist
21 master node or not running xdist at all.
22 """
23 return not hasattr(config, "workerinput")
26def pytest_configure(config: pytest.Config) -> None:
27 """Add some additional (global) configuration steps for pytest."""
28 # Workaround some issues loading emukit in certain environments.
29 if os.environ.get("DISPLAY", None):
30 try:
31 import matplotlib # pylint: disable=import-outside-toplevel
33 matplotlib.rcParams["backend"] = "agg"
34 if is_master(config) or dict(getattr(config, "workerinput", {}))["workerid"] == "gw0":
35 # Only warn once.
36 warn(
37 UserWarning(
38 "DISPLAY environment variable is set, "
39 "which can cause problems in some setups (e.g. WSL). "
40 "Adjusting matplotlib backend to "
41 f"""'{matplotlib.rcParams["backend"]}' """
42 "to compensate."
43 )
44 )
45 except ImportError:
46 pass
48 # Set pandas display options to make inline tests stable.
49 import pandas # pylint: disable=import-outside-toplevel
51 pandas.options.display.width = 150
52 pandas.options.display.max_columns = 10
54 # Create a temporary directory for sharing files between master and worker nodes.
55 if is_master(config):
56 # Add it to the config so that it can passed to the worker nodes.
57 setattr(config, "shared_temp_dir", mkdtemp())
60def pytest_configure_node(node: WorkerController) -> None:
61 """Xdist hook used to inform workers of the location of the shared temp dir."""
62 workerinput: dict = getattr(node, "workerinput")
63 workerinput["shared_temp_dir"] = getattr(node.config, "shared_temp_dir")
66@pytest.fixture(scope="session")
67def shared_temp_dir(request: pytest.FixtureRequest) -> str:
68 """Returns a unique and temporary directory which can be shared by master or worker
69 nodes in xdist runs.
70 """
71 if is_master(request.config):
72 return str(getattr(request.config, "shared_temp_dir"))
73 else:
74 workerinput: dict = getattr(request.config, "workerinput")
75 return str(workerinput["shared_temp_dir"])
78def pytest_unconfigure(config: pytest.Config) -> None:
79 """Called after all tests have completed."""
80 if is_master(config):
81 shared_tmp_dir = getattr(config, "shared_temp_dir", None)
82 if shared_tmp_dir:
83 shutil.rmtree(str(shared_tmp_dir))
86@pytest.fixture(scope="session")
87def short_testrun_uid(testrun_uid: str) -> str:
88 """Shorten the unique test run id that xdist provides so we can use it with other
89 systems (e.g., docker).
90 """
91 return testrun_uid[0:8]