-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathconftest.py
More file actions
90 lines (75 loc) · 2.83 KB
/
conftest.py
File metadata and controls
90 lines (75 loc) · 2.83 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
from typing import TYPE_CHECKING, Any
import pytest
from numpy.random import default_rng, randint
def pytest_addoption(parser: pytest.Parser):
parser.addoption("--long", action="store_false", help="If set, long tests will run")
parser.addoption(
"--long-costly",
action="store_false",
help="If set, long tests that cost credit will run",
)
parser.addoption(
"--long-local",
action="store_false",
help="If set, only local long tests will run",
)
parser.addoption(
"--seed",
action="store",
default=None,
type=int,
help="Set a global random seed for tests (default is None for random behavior).",
)
parser.addoption(
"--providers",
action="store",
nargs="*",
type=str,
help="List of providers to enable (e.g. --providers cirq qiskit azure)",
)
def pytest_configure(config: Any):
"""
Allows plugins and conftest files to perform initial configuration.
This hook is called for every plugin and initial conftest
file after command line options have been parsed.
"""
if (
not config.getoption("--long")
or not config.getoption("--long-costly")
or not config.getoption("--long-local")
):
from tests.local_storage.test_local_storage import create_test_local_storage
providers = config.getoption("--providers")
if TYPE_CHECKING:
assert isinstance(providers, list) or isinstance(providers, type(None))
if providers is None:
providers = ["all"]
elif not providers:
providers = []
print("Creating local storage for tests")
create_test_local_storage(providers)
@pytest.fixture(autouse=True)
def mock_random(monkeypatch: pytest.MonkeyPatch, request: pytest.FixtureRequest):
seed = request.config.getoption("--seed")
if TYPE_CHECKING:
assert isinstance(seed, int) or isinstance(seed, type(None))
if seed is None:
seed = randint(0, 1024)
print(f"Using seed {seed}")
def stable_random(*args: Any, **kwargs: Any):
user_seed = args[0] if len(args) != 0 else None
return default_rng(user_seed or seed)
monkeypatch.setattr('numpy.random.default_rng', stable_random)
def pytest_runtest_setup(item: pytest.Function):
providers = item.config.getoption("--providers")
if TYPE_CHECKING:
assert isinstance(providers, list) or isinstance(providers, type(None))
if providers is None:
providers = ["all"]
elif not providers:
providers = []
provider_marker = item.get_closest_marker("provider")
if provider_marker:
required = provider_marker.args[0]
if "all" not in providers and required not in providers:
pytest.skip(f"Skipping test: provider '{required}' not active")