-
Notifications
You must be signed in to change notification settings - Fork 22
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
Fix skipTest
inside subTest
#169
Merged
Merged
Changes from all commits
Commits
Show all changes
31 commits
Select commit
Hold shift + click to select a range
c62d3ae
fix
ydshieh c5c02fa
fix more: non subtest skip cases
ydshieh a439226
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 866c990
fix: # type: ignore[method-assign]
ydshieh 590624d
Merge branch 'fix_skip' of https://github.com/ydshieh/pytest-subtests…
ydshieh a737260
fix more: # type: ignore[attr-defined]
ydshieh 2e38614
fix more: # type: ignore[attr-defined]
ydshieh d7f0fa5
fix more: # type: ignore[attr-defined]
ydshieh aa4a17b
fix more: # type: ignore[attr-defined]
ydshieh d6e9e5e
fix more: # type: ignore[attr-defined]
ydshieh a99ea3b
fix None exec_info in subtest error
ydshieh 07ef703
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 67993fe
remove copy
ydshieh 23929b5
style
ydshieh 7461dd6
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 92e427c
comment
ydshieh a9c852e
fix python versions
ydshieh f9c0fba
fix recursion
ydshieh aecfe7a
fix recursion
ydshieh 87fcb67
Add first test
ydshieh 01c2b12
Add first test
ydshieh 450e81e
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] fd94cb9
add more tests
ydshieh f75438d
add more tests
ydshieh d14628b
Merge branch 'fix_skip' of https://github.com/ydshieh/pytest-subtests…
ydshieh 15612c0
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 9c4af2d
Fix tests due to column size
nicoddemus 3b6c4ff
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 35711d8
Update CHANGELOG
nicoddemus 475a356
Fix tests due to column size
nicoddemus da27259
Import protected _SubTest locally
nicoddemus 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 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 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 |
---|---|---|
|
@@ -98,6 +98,29 @@ def _from_test_report(cls, test_report: TestReport) -> SubTestReport: | |
return super()._from_json(test_report._to_json()) | ||
|
||
|
||
def _addSkip(self: TestCaseFunction, testcase: TestCase, reason: str) -> None: | ||
from unittest.case import _SubTest # type: ignore[attr-defined] | ||
|
||
if isinstance(testcase, _SubTest): | ||
self._originaladdSkip(testcase, reason) # type: ignore[attr-defined] | ||
if self._excinfo is not None: | ||
exc_info = self._excinfo[-1] | ||
self.addSubTest(testcase.test_case, testcase, exc_info) # type: ignore[attr-defined] | ||
else: | ||
# For python < 3.11: the non-subtest skips have to be added by `_originaladdSkip` only after all subtest | ||
# failures are processed by `_addSubTest`. | ||
if sys.version_info < (3, 11): | ||
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. python unittest has some changes (since 3.11) which leads to this if/else here |
||
subtest_errors = [ | ||
x | ||
for x, y in self.instance._outcome.errors | ||
if isinstance(x, _SubTest) and y is not None | ||
] | ||
if len(subtest_errors) == 0: | ||
self._originaladdSkip(testcase, reason) # type: ignore[attr-defined] | ||
else: | ||
self._originaladdSkip(testcase, reason) # type: ignore[attr-defined] | ||
|
||
|
||
def _addSubTest( | ||
self: TestCaseFunction, | ||
test_case: Any, | ||
|
@@ -122,10 +145,41 @@ def _addSubTest( | |
node=self, call=call_info, report=sub_report | ||
) | ||
|
||
# For python < 3.11: add non-subtest skips once all subtest failures are processed by # `_addSubTest`. | ||
if sys.version_info < (3, 11): | ||
from unittest.case import _SubTest # type: ignore[attr-defined] | ||
|
||
non_subtest_skip = [ | ||
(x, y) | ||
for x, y in self.instance._outcome.skipped | ||
if not isinstance(x, _SubTest) | ||
] | ||
subtest_errors = [ | ||
(x, y) | ||
for x, y in self.instance._outcome.errors | ||
if isinstance(x, _SubTest) and y is not None | ||
] | ||
# Check if we have non-subtest skips: if there are also sub failures, non-subtest skips are not treated in | ||
# `_addSubTest` and have to be added using `_originaladdSkip` after all subtest failures are processed. | ||
if len(non_subtest_skip) > 0 and len(subtest_errors) > 0: | ||
# Make sure we have processed the last subtest failure | ||
last_subset_error = subtest_errors[-1] | ||
if exc_info is last_subset_error[-1]: | ||
# Add non-subtest skips (as they could not be treated in `_addSkip`) | ||
for testcase, reason in non_subtest_skip: | ||
self._originaladdSkip(testcase, reason) # type: ignore[attr-defined] | ||
|
||
|
||
def pytest_configure(config: pytest.Config) -> None: | ||
TestCaseFunction.addSubTest = _addSubTest # type: ignore[attr-defined] | ||
TestCaseFunction.failfast = False # type: ignore[attr-defined] | ||
# This condition is to prevent `TestCaseFunction._originaladdSkip` being assigned again in a subprocess from a | ||
# parent python process where `addSkip` is already `_addSkip`. A such case is when running tests in | ||
# `test_subtests.py` where `pytester.runpytest` is used. Without this guard condition, `_originaladdSkip` is | ||
# assigned to `_addSkip` which is wrong as well as causing an infinite recursion in some cases. | ||
if not hasattr(TestCaseFunction, "_originaladdSkip"): | ||
TestCaseFunction._originaladdSkip = TestCaseFunction.addSkip # type: ignore[attr-defined] | ||
TestCaseFunction.addSkip = _addSkip # type: ignore[method-assign] | ||
|
||
# Hack (#86): the terminal does not know about the "subtests" | ||
# status, so it will by default turn the output to yellow. | ||
|
@@ -154,6 +208,9 @@ def pytest_unconfigure() -> None: | |
del TestCaseFunction.addSubTest | ||
if hasattr(TestCaseFunction, "failfast"): | ||
del TestCaseFunction.failfast | ||
if hasattr(TestCaseFunction, "_originaladdSkip"): | ||
TestCaseFunction.addSkip = TestCaseFunction._originaladdSkip # type: ignore[method-assign] | ||
del TestCaseFunction._originaladdSkip | ||
|
||
|
||
@pytest.fixture | ||
|
This file contains 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 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
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.
It seems we don't really need call
self._originaladdSkip
for subtest skips, i.e. the following also works