-
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
Open
mhucka
wants to merge
7
commits into
quantumlib:main
Choose a base branch
from
mhucka:skipslow
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+61
−5
Open
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
8c6aa57
Add pytest flag `--skipslow` to `conftest.py`
mhucka 44f76c0
Use new `--skipslow` flag
mhucka 0468d66
Update conftest.py
mhucka 40acbd4
Merge branch 'main' into skipslow
mhucka 79ee4fd
Merge branch 'main' into skipslow
mhucka ad5bbfd
Add more tests for better coverage
mhucka dd38780
Format
mhucka File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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" | ||
| ) | ||
|
|
||
| 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) |
||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.