Skip to content

Commit 0104788

Browse files
committed
unittest: Allow SkipTest to work within a subTest.
Signed-off-by: Damien George <damien@micropython.org>
1 parent 0827a31 commit 0104788

File tree

2 files changed

+21
-6
lines changed

2 files changed

+21
-6
lines changed
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import unittest
2+
3+
4+
class Test(unittest.TestCase):
5+
def test_subtest_skip(self):
6+
for i in range(4):
7+
with self.subTest(i=i):
8+
print("sub test", i)
9+
if i == 2:
10+
self.skipTest("skip 2")
11+
12+
13+
if __name__ == "__main__":
14+
unittest.main()

python-stdlib/unittest/unittest/__init__.py

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -348,7 +348,13 @@ def _handle_test_exception(
348348
exc = exc_info[1]
349349
traceback = exc_info[2]
350350
ex_str = _capture_exc(exc, traceback)
351-
if isinstance(exc, AssertionError):
351+
if isinstance(exc, SkipTest):
352+
reason = exc.args[0]
353+
test_result.skippedNum += 1
354+
test_result.skipped.append((current_test, reason))
355+
print(" skipped:", reason)
356+
return
357+
elif isinstance(exc, AssertionError):
352358
test_result.failuresNum += 1
353359
test_result.failures.append((current_test, ex_str))
354360
if verbose:
@@ -396,11 +402,6 @@ def run_one(test_function):
396402
print(" FAIL")
397403
else:
398404
print(" ok")
399-
except SkipTest as e:
400-
reason = e.args[0]
401-
print(" skipped:", reason)
402-
test_result.skippedNum += 1
403-
test_result.skipped.append((name, c, reason))
404405
except Exception as ex:
405406
_handle_test_exception(
406407
current_test=(name, c), test_result=test_result, exc_info=(type(ex), ex, None)

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy