diff --git a/cirq-rigetti/cirq_rigetti/conftest.py b/cirq-rigetti/cirq_rigetti/conftest.py index 3834fa063c3..da647f43ac8 100644 --- a/cirq-rigetti/cirq_rigetti/conftest.py +++ b/cirq-rigetti/cirq_rigetti/conftest.py @@ -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") diff --git a/conftest.py b/conftest.py index 885e0b55f7b..d61f5277b2b 100644 --- a/conftest.py +++ b/conftest.py @@ -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]) diff --git a/dev_tools/conftest.py b/dev_tools/conftest.py index 7dbde3b3ef4..2e7bb5fc556 100644 --- a/dev_tools/conftest.py +++ b/dev_tools/conftest.py @@ -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. diff --git a/pyproject.toml b/pyproject.toml index 59af89effc1..1c57fe299b6 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -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.", +]