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