Skip to content

Commit 5584285

Browse files
authored
Fix crash when unittest.skip is applied to the class (#174)
Fix #173
1 parent a1e9e24 commit 5584285

File tree

3 files changed

+41
-2
lines changed

3 files changed

+41
-2
lines changed

CHANGELOG.rst

+9
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,15 @@
11
CHANGELOG
22
=========
33

4+
UNRELEASED
5+
----------
6+
7+
*UNRELEASED*
8+
9+
* Fix ``self.instance._outcome`` is ``None`` case in #173 (`#174`_).
10+
11+
.. _#174: https://github.com/pytest-dev/pytest-subtests/pull/174
12+
413
0.14.0
514
------
615

src/pytest_subtests/plugin.py

+4-2
Original file line numberDiff line numberDiff line change
@@ -105,8 +105,10 @@ def _addSkip(self: TestCaseFunction, testcase: TestCase, reason: str) -> None:
105105
self.addSubTest(testcase.test_case, testcase, exc_info) # type: ignore[attr-defined]
106106
else:
107107
# For python < 3.11: the non-subtest skips have to be added by `_originaladdSkip` only after all subtest
108-
# failures are processed by `_addSubTest`.
109-
if sys.version_info < (3, 11):
108+
# failures are processed by `_addSubTest`. (`self.instance._outcome` has no attribute `skipped/errors` anymore.)
109+
# For python < 3.11, we also need to check if `self.instance._outcome` is `None` (this happens if the test
110+
# class/method is decorated with `unittest.skip`, see #173).
111+
if sys.version_info < (3, 11) and self.instance._outcome is not None:
110112
subtest_errors = [
111113
x
112114
for x, y in self.instance._outcome.errors

tests/test_subtests.py

+28
Original file line numberDiff line numberDiff line change
@@ -336,6 +336,34 @@ def test_foo(self):
336336
["collected 1 item", "* 3 xfailed, 1 passed in *"]
337337
)
338338

339+
@pytest.mark.parametrize("runner", ["pytest-normal"])
340+
def test_only_original_skip_is_called(
341+
self,
342+
pytester: pytest.Pytester,
343+
monkeypatch: pytest.MonkeyPatch,
344+
runner: Literal["pytest-normal"],
345+
) -> None:
346+
"""Regression test for #173."""
347+
monkeypatch.setenv("COLUMNS", "200")
348+
p = pytester.makepyfile(
349+
"""
350+
import unittest
351+
from unittest import TestCase, main
352+
353+
@unittest.skip("skip this test")
354+
class T(unittest.TestCase):
355+
def test_foo(self):
356+
assert 1 == 2
357+
358+
if __name__ == '__main__':
359+
main()
360+
"""
361+
)
362+
result = pytester.runpytest(p, "-v", "-rsf")
363+
result.stdout.fnmatch_lines(
364+
["SKIPPED [1] test_only_original_skip_is_called.py:6: skip this test"]
365+
)
366+
339367
@pytest.mark.parametrize("runner", ["unittest", "pytest-normal", "pytest-xdist"])
340368
def test_skip_with_failure(
341369
self,

0 commit comments

Comments
 (0)