Skip to content

Update tests to use test.support.captured_std* #136632

Open
@inventshah

Description

@inventshah

Python's test suite has test.support.captured_std* utilities to temporarily swap sys.std* with an io.StringIO(). These wrapper functions are used at least 240 times throughout the tests. A few greps1 show quite a few instances where these helpers could be used but are not (45 use contextlib.redirect_std*, 30 use test.support.captured_output("std*"), and 15 use a manual solution).

Should we update these calls to use the support utilities to improve consistency or is it not worth the risk of accidentally changing a test's meaning and cluttering git blame? I found 31d1d72 from last year which did similar updating in test_pydoc/test_pydoc.py. There's also an argument the "update" should go from test.support.captured_std* to the newer contextlib.redirect_std* instead, and remove the testing specific utilities.

Since this touches a wide range of domains, my thought was to split this up into separate PRs for each test file so the appropriate person can more easily review. But we can also do this in larger chunks, at least for the lower risk changes.

Instances of contextlib.redirect_std* swappable for test.support.captured_std*. In some cases allowing the removal of io and contextlib imports.

Instances of test.support.captured_output("std*") swappable for a corresponding test.support.captured_std*. Low risk to change since the latter calls the former.

Tests with a manual stream save/restore (most are stdin). Higher risk especially when replacing over long blocks.

  • libregrtest/single.py - L297
  • test_builtin.py - L1748
  • test_capi/check_config.py - L70
  • test_concurrent_futures/test_deadlock.py - L148
  • test_exceptions.py - L71
  • test_faulthandler.py - L399, L806
  • test_fileinput.py - L129, L151
  • test_optparse.py - L160
  • test_profile.py - L90 & L101 call silent which could be test.support.captured_stdout instead of reimplementing the function.
  • test_pydoc.py - L478
  • test_unittest/test_runner.py - L1425
  • test_winconsoleio.py - L170
  • test_wsgiref.py - L74

Linked PRs

Footnotes

  1. Searched for captured_output("std, sys.std* =, and redirect_std* so there may be some missing instances. I manually reviewed the ones listed to check if replacement seemed doable (for instance test_calendar.py needs TextIOWrapper(BytesIO) not StringIO, so cannot be easily swapped).

Metadata

Metadata

Assignees

No one assigned

    Labels

    testsTests in the Lib/test dirtype-refactorCode refactoring (with no changes in behavior)

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions