Skip to content

Treat malformed unittest skips as collection errors#14732

Closed
zhangli091011 wants to merge 2 commits into
pytest-dev:mainfrom
zhangli091011:fix-unittest-skip-import-error
Closed

Treat malformed unittest skips as collection errors#14732
zhangli091011 wants to merge 2 commits into
pytest-dev:mainfrom
zhangli091011:fix-unittest-skip-import-error

Conversation

@zhangli091011

@zhangli091011 zhangli091011 commented Jul 20, 2026

Copy link
Copy Markdown

Closes #10821.

Summary

Malformed use of an aliased unittest.skip decorator currently raises unittest.SkipTest from unittest's generated skip_wrapper while importing the test module. Pytest classifies every collection-time unittest.SkipTest as a module skip, so this programming error can hide all tests in that module.

This keeps legitimate unittest module skipping intact and changes only SkipTest whose innermost traceback frame is unittest's generated skip_wrapper. Such exceptions are now normal collection errors. Direct module-level raise unittest.SkipTest(...) remains a skipped module, matching unittest.TestLoader.discover, and pytest.skip(..., allow_module_level=True) is unaffected.

This narrower boundary addresses the compatibility concern raised on #10864 and #14415 rather than removing collection-time SkipTest handling wholesale.

Tests

  • python -m pytest testing/test_unittest.py testing/test_runner.py testing/test_skipping.py (231 passed, 9 skipped, 1 xfailed)
  • tox -e py310 -- testing/test_unittest.py -k "raising_unittest_skiptest_during_collection or unittest_skip_decorator_misuse_is_collection_error" (2 passed)
  • python -m pytest testing/test_unittest.py -k "raising_unittest_skiptest_during_collection or unittest_skip_decorator_misuse_is_collection_error" on Python 3.13 (2 passed)
  • pre-commit run --all-files --show-diff-on-failure (passed, including Ruff, formatting, mypy, RST and policy hooks)

Current main was reproduced separately: the issue example reports collected 0 items / 1 skipped before this change.

AI assistance

OpenCode assisted with repository inspection, implementation, and validation, and is credited by a Co-authored-by trailer. I reviewed the complete diff and tests, understand the traceback-based compatibility boundary, and take responsibility for this change and reviewer follow-up in accordance with pytest's AI/LLM-Assisted Contributions Policy.

  • Includes a regression test.
  • Includes a changelog entry.
  • Adds the contributor to AUTHORS.
  • Allows maintainers to push and squash when merging.
  • Credits the AI agent in the commit trailer.

Preserve deliberate unittest.SkipTest module skips while reporting SkipTest raised by unittest's generated skip decorator wrapper as an import-time collection error.

Co-authored-by: OpenCode <opencode@users.noreply.github.com>
@psf-chronographer psf-chronographer Bot added the bot:chronographer:provided (automation) changelog entry is part of PR label Jul 20, 2026
Twisted replaces unittest.case.SkipTest during its integration tests, while preserving the collection-error behavior under test.

Co-authored-by: OpenCode <opencode@users.noreply.github.com>
@The-Compiler

Copy link
Copy Markdown
Member

The submitting account owner must review, understand, and take responsibility for this change

This... doesn't exactly convince me that this actually happened?

@zhangli091011

This comment was marked as low quality.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bot:chronographer:provided (automation) changelog entry is part of PR

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Unexpected behavior of skipping entire module when calling unittest.skip("a")("b")

3 participants