diff --git a/python-stdlib/unittest/tests/test_subtest.py b/python-stdlib/unittest/tests/test_subtest.py new file mode 100644 index 000000000..324150e27 --- /dev/null +++ b/python-stdlib/unittest/tests/test_subtest.py @@ -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() diff --git a/python-stdlib/unittest/unittest/__init__.py b/python-stdlib/unittest/unittest/__init__.py index f15f30448..6d7fa40b8 100644 --- a/python-stdlib/unittest/unittest/__init__.py +++ b/python-stdlib/unittest/unittest/__init__.py @@ -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: @@ -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)