-
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
Changes from 12 commits
c62d3ae
c5c02fa
a439226
866c990
590624d
a737260
2e38614
d7f0fa5
aa4a17b
d6e9e5e
a99ea3b
07ef703
67993fe
23929b5
7461dd6
92e427c
a9c852e
f9c0fba
aecfe7a
87fcb67
01c2b12
450e81e
fd94cb9
f75438d
d14628b
15612c0
9c4af2d
3b6c4ff
35711d8
475a356
da27259
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 |
---|---|---|
@@ -1,5 +1,6 @@ | ||
from __future__ import annotations | ||
|
||
import copy | ||
import sys | ||
import time | ||
from contextlib import contextmanager | ||
|
@@ -13,6 +14,7 @@ | |
from typing import Mapping | ||
from typing import TYPE_CHECKING | ||
from unittest import TestCase | ||
from unittest.case import _SubTest # type: ignore[attr-defined] | ||
|
||
import attr | ||
import pluggy | ||
|
@@ -98,6 +100,28 @@ 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: | ||
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: | ||
# The non-subtest skips have to be added by `_originaladdSkip` only after all subtest failures are processed by | ||
# `_addSubTest`. | ||
if ( | ||
len( | ||
[ | ||
x | ||
for x, y in self.instance._outcome.errors | ||
if isinstance(x, _SubTest) and y is not None | ||
] | ||
ydshieh marked this conversation as resolved.
Show resolved
Hide resolved
|
||
) | ||
== 0 | ||
): | ||
self._originaladdSkip(testcase, reason) # type: ignore[attr-defined] | ||
|
||
|
||
def _addSubTest( | ||
self: TestCaseFunction, | ||
test_case: Any, | ||
|
@@ -122,10 +146,29 @@ def _addSubTest( | |
node=self, call=call_info, report=sub_report | ||
) | ||
|
||
# Add non-subtest skips once all subtest failures are processed by # `_addSubTest`. | ||
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 | ||
] | ||
if len(subtest_errors) > 0 and len(non_subtest_skip) > 0: | ||
ydshieh marked this conversation as resolved.
Show resolved
Hide resolved
|
||
last_subset_error = subtest_errors[-1] | ||
if exc_info is last_subset_error[-1]: | ||
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] | ||
TestCaseFunction._originaladdSkip = copy.copy(TestCaseFunction.addSkip) # type: ignore[attr-defined] | ||
ydshieh marked this conversation as resolved.
Show resolved
Hide resolved
|
||
TestCaseFunction.addSkip = _addSkip # type: ignore[method-assign] | ||
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. The Unlike The |
||
|
||
# Hack (#86): the terminal does not know about the "subtests" | ||
# status, so it will by default turn the output to yellow. | ||
|
@@ -154,6 +197,9 @@ def pytest_unconfigure() -> None: | |
del TestCaseFunction.addSubTest | ||
if hasattr(TestCaseFunction, "failfast"): | ||
del TestCaseFunction.failfast | ||
if hasattr(TestCaseFunction, "_originaladdSkip"): | ||
TestCaseFunction.addSkip = copy.copy(TestCaseFunction._originaladdSkip) # type: ignore[method-assign] | ||
ydshieh marked this conversation as resolved.
Show resolved
Hide resolved
|
||
del TestCaseFunction._originaladdSkip | ||
|
||
|
||
@pytest.fixture | ||
|
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