-
Notifications
You must be signed in to change notification settings - Fork 430
Support using flag --skipslow instead of -m "not slow" for pytest
#1421
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
8c6aa57
44f76c0
0468d66
40acbd4
79ee4fd
ad5bbfd
dd38780
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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") | ||
|
Comment on lines
+75
to
+77
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This reports $ pytest -rs --skipslow src/openfermion/ops/representations/doci_hamiltonian_test.py::IntegralTransformsTest::test_fermionic_hamiltonian_from_integrals
...
================================================== short test summary info ===================================================
SKIPPED [1] conftest.py:77: skipped because of --skipslow option
===================================================== 1 skipped in 0.06s =====================================================I think it is better to use the $ pytest -rs dev_tools/notebooks/notebook_test.py
...
================================================== short test summary info ===================================================
SKIPPED [59] dev_tools/notebooks/notebook_test.py:101: need --enable-slow-tests option to run
=============================================== 2 passed, 59 skipped in 0.17s ================================================
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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" | ||
| ) | ||
|
Comment on lines
+175
to
+185
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There seems to be too much mockery and AI generated crud for this test to be meaningful. It verifies that conftest hook functions do what they do on their arguments, but that tells nothing of if the hooks are used in a pytest session and if they have desired effects. I suggest to delete this; it is a second order test-of-a-test-code anyway. |
||
|
|
||
| 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) | ||
|
Comment on lines
+187
to
+219
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Since we are updating 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.get_closest_marker.return_value = MagicMock()
item.config.getoption.return_value = True
with self.assertRaises(pytest.skip.Exception):
conftest.pytest_runtest_setup(item)
item.get_closest_marker.assert_called_once_with("slow")
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.get_closest_marker.return_value = None
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.get_closest_marker.return_value = MagicMock()
item.config.getoption.return_value = False
conftest.pytest_runtest_setup(item) |
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Using
"slow" in item.keywordsis a pytest anti-pattern.item.keywordscontains not only the markers applied to a test, but also the names of the test function, class, and module (and their components split by underscores). This means any test with the wordslowin its name, class, or file name (e.g.,test_slow_algorithm) will be incorrectly skipped when--skipslowis passed, even if it is not marked with@pytest.mark.slow.Instead, use
item.get_closest_marker("slow")to safely and precisely check for the presence of the marker.