Skip to content

Commit

Permalink
Moved rigetti integration test logic to higher level conftest.py file… (
Browse files Browse the repository at this point in the history
#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 <juhas@google.com>
  • Loading branch information
nafaynajam and pavoljuhas authored Oct 27, 2023
1 parent 1549785 commit 1eced48
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 42 deletions.
21 changes: 0 additions & 21 deletions cirq-rigetti/cirq_rigetti/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,27 +30,6 @@
import sympy
import numpy as np


def pytest_collection_modifyitems(config, items): # pragma: no cover
# do not skip integration tests if --rigetti-integration option passed
if config.getoption('--rigetti-integration'):
return
# do not skip integration tests rigetti_integration marker explicitly passed.
if 'rigetti_integration' in config.getoption('-m'):
return
# otherwise skip all tests marked "rigetti_integration".
skip_rigetti_integration = pytest.mark.skip(reason="need --rigetti-integration option to run")
for item in items:
if "rigetti_integration" in item.keywords:
item.add_marker(skip_rigetti_integration)


def pytest_configure(config):
config.addinivalue_line(
"markers", "rigetti_integration: tests that connect to Quil compiler or QVM."
)


T = TypeVar("T")


Expand Down
29 changes: 29 additions & 0 deletions conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,32 @@ def pytest_addoption(parser):
default=False,
help="run Rigetti integration tests",
)
parser.addoption(
"--enable-slow-tests", action="store_true", default=False, help="run slow tests"
)


def pytest_collection_modifyitems(config, items):
# Let pytest handle markexpr if present. Make an exception for
# `pytest --co -m skip` so we can check test skipping rules below.
markexpr_words = frozenset(config.option.markexpr.split())
if not markexpr_words.issubset(["not", "skip"]):
return # pragma: no cover

# our marks for tests to be skipped by default
skip_marks = {
"rigetti_integration": pytest.mark.skip(reason="need --rigetti-integration option to run"),
"slow": pytest.mark.skip(reason="need --enable-slow-tests option to run"),
"weekly": pytest.mark.skip(reason='only run by weekly automation'),
}

# drop skip_marks for tests enabled by command line options
if config.option.rigetti_integration:
del skip_marks["rigetti_integration"] # pragma: no cover
if config.option.enable_slow_tests:
del skip_marks["slow"] # pragma: no cover
skip_keywords = frozenset(skip_marks.keys())

for item in items:
for k in skip_keywords.intersection(item.keywords):
item.add_marker(skip_marks[k])
21 changes: 0 additions & 21 deletions dev_tools/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,27 +26,6 @@
from dev_tools.env_tools import create_virtual_env


def pytest_configure(config):
config.addinivalue_line("markers", "slow: mark tests as slow")
config.addinivalue_line("markers", "weekly: mark tests as run only by weekly automation")


def pytest_collection_modifyitems(config, items):
markexpr = config.option.markexpr
if markexpr:
return # let pytest handle this

skip_slow_marker = pytest.mark.skip(reason='slow marker not selected')
for item in items:
if 'slow' in item.keywords:
item.add_marker(skip_slow_marker)

skip_weekly_marker = pytest.mark.skip(reason='only run by weekly automation')
for item in items:
if 'weekly' in item.keywords:
item.add_marker(skip_weekly_marker)


@pytest.fixture(scope="session")
def cloned_env(testrun_uid, worker_id):
"""Fixture to allow tests to run in a clean virtual env.
Expand Down
5 changes: 5 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,8 @@ skip-magic-trailing-comma = true
filterwarnings = [
"ignore:Matplotlib is currently using agg:UserWarning",
]
markers = [
"rigetti_integration: tests that connect to Quil compiler or QVM.",
"slow: slow tests that should be skipped by default.",
"weekly: tests to be run only by weekly CI automation.",
]

0 comments on commit 1eced48

Please sign in to comment.