Skip to content

Commit 1eced48

Browse files
Moved rigetti integration test logic to higher level conftest.py file… (#6328)
* Move all pytest_collection_modifyitems hooks to the top conftest.py and apply mark-based test-selection there * Move custom marks definitions to pyproject.toml so they show in `pytest --markers` * Add custom pytest option `--enable-slow-tests` to run slow-marked tests. These are otherwise skipped by default. * Support a quick check of test selection using `pytest --co -m skip` and `pytest --co -m "not skip"` Partially implements #6211 --------- Co-authored-by: Pavol Juhas <[email protected]>
1 parent 1549785 commit 1eced48

File tree

4 files changed

+34
-42
lines changed

4 files changed

+34
-42
lines changed

cirq-rigetti/cirq_rigetti/conftest.py

-21
Original file line numberDiff line numberDiff line change
@@ -30,27 +30,6 @@
3030
import sympy
3131
import numpy as np
3232

33-
34-
def pytest_collection_modifyitems(config, items): # pragma: no cover
35-
# do not skip integration tests if --rigetti-integration option passed
36-
if config.getoption('--rigetti-integration'):
37-
return
38-
# do not skip integration tests rigetti_integration marker explicitly passed.
39-
if 'rigetti_integration' in config.getoption('-m'):
40-
return
41-
# otherwise skip all tests marked "rigetti_integration".
42-
skip_rigetti_integration = pytest.mark.skip(reason="need --rigetti-integration option to run")
43-
for item in items:
44-
if "rigetti_integration" in item.keywords:
45-
item.add_marker(skip_rigetti_integration)
46-
47-
48-
def pytest_configure(config):
49-
config.addinivalue_line(
50-
"markers", "rigetti_integration: tests that connect to Quil compiler or QVM."
51-
)
52-
53-
5433
T = TypeVar("T")
5534

5635

conftest.py

+29
Original file line numberDiff line numberDiff line change
@@ -22,3 +22,32 @@ def pytest_addoption(parser):
2222
default=False,
2323
help="run Rigetti integration tests",
2424
)
25+
parser.addoption(
26+
"--enable-slow-tests", action="store_true", default=False, help="run slow tests"
27+
)
28+
29+
30+
def pytest_collection_modifyitems(config, items):
31+
# Let pytest handle markexpr if present. Make an exception for
32+
# `pytest --co -m skip` so we can check test skipping rules below.
33+
markexpr_words = frozenset(config.option.markexpr.split())
34+
if not markexpr_words.issubset(["not", "skip"]):
35+
return # pragma: no cover
36+
37+
# our marks for tests to be skipped by default
38+
skip_marks = {
39+
"rigetti_integration": pytest.mark.skip(reason="need --rigetti-integration option to run"),
40+
"slow": pytest.mark.skip(reason="need --enable-slow-tests option to run"),
41+
"weekly": pytest.mark.skip(reason='only run by weekly automation'),
42+
}
43+
44+
# drop skip_marks for tests enabled by command line options
45+
if config.option.rigetti_integration:
46+
del skip_marks["rigetti_integration"] # pragma: no cover
47+
if config.option.enable_slow_tests:
48+
del skip_marks["slow"] # pragma: no cover
49+
skip_keywords = frozenset(skip_marks.keys())
50+
51+
for item in items:
52+
for k in skip_keywords.intersection(item.keywords):
53+
item.add_marker(skip_marks[k])

dev_tools/conftest.py

-21
Original file line numberDiff line numberDiff line change
@@ -26,27 +26,6 @@
2626
from dev_tools.env_tools import create_virtual_env
2727

2828

29-
def pytest_configure(config):
30-
config.addinivalue_line("markers", "slow: mark tests as slow")
31-
config.addinivalue_line("markers", "weekly: mark tests as run only by weekly automation")
32-
33-
34-
def pytest_collection_modifyitems(config, items):
35-
markexpr = config.option.markexpr
36-
if markexpr:
37-
return # let pytest handle this
38-
39-
skip_slow_marker = pytest.mark.skip(reason='slow marker not selected')
40-
for item in items:
41-
if 'slow' in item.keywords:
42-
item.add_marker(skip_slow_marker)
43-
44-
skip_weekly_marker = pytest.mark.skip(reason='only run by weekly automation')
45-
for item in items:
46-
if 'weekly' in item.keywords:
47-
item.add_marker(skip_weekly_marker)
48-
49-
5029
@pytest.fixture(scope="session")
5130
def cloned_env(testrun_uid, worker_id):
5231
"""Fixture to allow tests to run in a clean virtual env.

pyproject.toml

+5
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,8 @@ skip-magic-trailing-comma = true
88
filterwarnings = [
99
"ignore:Matplotlib is currently using agg:UserWarning",
1010
]
11+
markers = [
12+
"rigetti_integration: tests that connect to Quil compiler or QVM.",
13+
"slow: slow tests that should be skipped by default.",
14+
"weekly: tests to be run only by weekly CI automation.",
15+
]

0 commit comments

Comments
 (0)