Skip to content

Commit

Permalink
unittest: Allow SkipTest to work within a subTest.
Browse files Browse the repository at this point in the history
Signed-off-by: Damien George <[email protected]>
  • Loading branch information
dpgeorge committed Nov 7, 2024
1 parent 0827a31 commit 0104788
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 6 deletions.
14 changes: 14 additions & 0 deletions python-stdlib/unittest/tests/test_subtest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import unittest


class Test(unittest.TestCase):
def test_subtest_skip(self):
for i in range(4):
with self.subTest(i=i):
print("sub test", i)
if i == 2:
self.skipTest("skip 2")


if __name__ == "__main__":
unittest.main()
13 changes: 7 additions & 6 deletions python-stdlib/unittest/unittest/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -348,7 +348,13 @@ def _handle_test_exception(
exc = exc_info[1]
traceback = exc_info[2]
ex_str = _capture_exc(exc, traceback)
if isinstance(exc, AssertionError):
if isinstance(exc, SkipTest):
reason = exc.args[0]
test_result.skippedNum += 1
test_result.skipped.append((current_test, reason))
print(" skipped:", reason)
return
elif isinstance(exc, AssertionError):
test_result.failuresNum += 1
test_result.failures.append((current_test, ex_str))
if verbose:
Expand Down Expand Up @@ -396,11 +402,6 @@ def run_one(test_function):
print(" FAIL")
else:
print(" ok")
except SkipTest as e:
reason = e.args[0]
print(" skipped:", reason)
test_result.skippedNum += 1
test_result.skipped.append((name, c, reason))
except Exception as ex:
_handle_test_exception(
current_test=(name, c), test_result=test_result, exc_info=(type(ex), ex, None)
Expand Down

0 comments on commit 0104788

Please sign in to comment.