Skip to content

unittest: Allow SkipTest to work within a subTest. #935

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

Merged
merged 2 commits into from
Nov 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# This makes unittest return an error code, so is not named "test_xxx.py".

import unittest


Expand Down
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
2 changes: 2 additions & 0 deletions tools/ci.sh
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ function ci_package_tests_run {
python-stdlib/os-path/test_path.py \
python-stdlib/pickle/test_pickle.py \
python-stdlib/string/test_translate.py \
python-stdlib/unittest/tests/exception.py \
unix-ffi/gettext/test_gettext.py \
unix-ffi/pwd/test_getpwnam.py \
unix-ffi/re/test_re.py \
Expand Down Expand Up @@ -90,6 +91,7 @@ function ci_package_tests_run {
python-stdlib/shutil \
python-stdlib/tempfile \
python-stdlib/time \
python-stdlib/unittest/tests \
python-stdlib/unittest-discover/tests \
; do
(cd $path && $MICROPYTHON -m unittest)
Expand Down
Loading