diff --git a/.github/workflows/ci.yaml b/.github/workflows/ci.yaml index 9c218399f..e642299f1 100644 --- a/.github/workflows/ci.yaml +++ b/.github/workflows/ci.yaml @@ -130,7 +130,7 @@ jobs: - name: Run pytest run: | echo '::add-matcher::.github/problem-matchers/pytest.json' - check/pytest -n auto -m "not slow" + check/pytest -n auto --skipslow pytest-extra: name: Extra unit tests @@ -162,7 +162,7 @@ jobs: - name: Run pytest run: | echo '::add-matcher::.github/problem-matchers/pytest.json' - check/pytest -n auto -m "not slow" src/openfermion/resource_estimates + check/pytest -n auto --skipslow src/openfermion/resource_estimates pytest-compat: name: Python compatibility tests @@ -190,7 +190,7 @@ jobs: - name: Run pytest run: | echo '::add-matcher::.github/problem-matchers/pytest.json' - check/pytest -n auto -m "not slow" + check/pytest -n auto --skipslow coverage: name: Python code coverage tests diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index e4b603e49..3db515805 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -293,7 +293,7 @@ We use [pytest](https://docs.pytest.org) to run our tests and in the `check/` subdirectory to run these programs. * During development, periodically check that code changes do not break anything. For fast checks, - run `check/pytest -m "not slow" -n auto PATH ...`, where `PATH ...` is one or more directories + run `check/pytest --skipslow -n auto PATH ...`, where `PATH ...` is one or more directories or `_test.py` files to test. (The `-n auto` option makes pytest use parallel processes; omit it if it causes problems on your system.) diff --git a/conftest.py b/conftest.py index 080bcba9d..3e50ce02f 100644 --- a/conftest.py +++ b/conftest.py @@ -66,3 +66,12 @@ def set_threadpool_limits(): yield else: yield + + +def pytest_addoption(parser: pytest.Parser) -> None: + parser.addoption("--skipslow", action="store_true", help="skips slow tests") + + +def pytest_runtest_setup(item: pytest.Item) -> None: + if "slow" in item.keywords and item.config.getoption("skipslow"): + pytest.skip("skipped because of --skipslow option") diff --git a/pyproject.toml b/pyproject.toml index 72ba61d15..b8aa9b48a 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -25,7 +25,7 @@ disable_warnings = ["no-sysmon"] [tool.pytest.ini_options] markers = [ - """slow: marks tests as slow (deselect with '-m "not slow"')""", + """slow: marks tests as slow (skip with '--skipslow')""", ] filterwarnings = [ "ignore:Skipped assert_qasm_is_consistent_with_unitary because qiskit.*:UserWarning", diff --git a/src/openfermion/config_test.py b/src/openfermion/config_test.py index c0fd7f79f..111f90411 100644 --- a/src/openfermion/config_test.py +++ b/src/openfermion/config_test.py @@ -170,3 +170,50 @@ def test_set_threading_limits(self): with patch("openfermion.config.get_available_cpu_count", return_value=8): set_threading_limits() self.assertEqual(os.environ.get("OMP_NUM_THREADS"), "7") + + +class ConftestTest(unittest.TestCase): + + def test_pytest_addoption(self): + import conftest + from unittest.mock import MagicMock + + parser = MagicMock() + conftest.pytest_addoption(parser) + parser.addoption.assert_called_once_with( + "--skipslow", action="store_true", help="skips slow tests" + ) + + def test_pytest_runtest_setup_skips(self): + import conftest + import pytest + from unittest.mock import MagicMock + + # Create mock item representing a slow test when skipslow is True. + item = MagicMock() + item.keywords = {"slow"} + item.config.getoption.return_value = True + + with self.assertRaises(pytest.skip.Exception): + conftest.pytest_runtest_setup(item) + + item.config.getoption.assert_called_once_with("skipslow") + + def test_pytest_runtest_setup_does_not_skip_if_not_slow(self): + import conftest + from unittest.mock import MagicMock + + # Test case 1: Not marked as 'slow', skipslow is True. + item = MagicMock() + item.keywords = set() + item.config.getoption.return_value = True + + # Should not raise an exception. + conftest.pytest_runtest_setup(item) + + # Test case 2: Marked as 'slow', skipslow is False. + item = MagicMock() + item.keywords = {"slow"} + item.config.getoption.return_value = False + + conftest.pytest_runtest_setup(item)