Skip to content

gh-108885: Report the examples of a doctest run by unittest - #155377

Open
serhiy-storchaka wants to merge 1 commit into
python:mainfrom
serhiy-storchaka:doctest-verbosity
Open

gh-108885: Report the examples of a doctest run by unittest#155377
serhiy-storchaka wants to merge 1 commit into
python:mainfrom
serhiy-storchaka:doctest-verbosity

Conversation

@serhiy-storchaka

Copy link
Copy Markdown
Member

DocTestCase.runTest() runs the examples with verbose=False, so there is no way to ask unittest for the details which doctest reports on its own. It now takes the verbosity from the test result, and reports every example if the test runner is asked for more than the test names.

$ ./python -m test -vv test_difflib
...
get_opcodes (difflib.SequenceMatcher)
Doctest: difflib.SequenceMatcher.get_opcodes ...
Trying:
    a = "qabxcd"
Expecting nothing
ok
...
get_opcodes (difflib.SequenceMatcher)
Doctest: difflib.SequenceMatcher.get_opcodes ... ok

The examples are written to the stream of the test runner, not to the standard output, so that they are not lost when the result buffers the output of the test.

Three changes make this reachable:

  • unittest.TestResult has now a verbosity attribute, which it accepted but ignored. TextTestRunner.run() sets it, next to failfast, buffer and tb_locals, because a result class is free to filter what its constructor gets: test.libregrtest does.
  • The unittest -v option is now counted, so that -vv means 3. It is parsed into the TestProgram, whose verbosity is already 1, so -v still means 2.
  • The verbosity of regrtest is one less, so it is translated where the test runner is created.
doctest output
regrtest, python -m unittest nothing
regrtest -v, python -m unittest -v the test name, as before
regrtest -vv, python -m unittest -vv every example

A counted option is relative, so -v passed twice now means 3 rather than 2, and -q -v means 1 rather than 2.

This is the only goal of the issue which is left: the quiet levels already behave as it asks, and GH-134890 made every example a subtest.

DocTestCase ran its examples with verbose=False, so there was no way to ask
unittest for the details which doctest reports on its own.  It now takes the
verbosity from the test result, and reports every example if the test runner
is asked for more than the test names.  They are written to the stream of the
test runner, so that they are not lost when it buffers the output of the test.

To make this reachable:

* unittest.TestResult has now a verbosity attribute, which it accepted but
  ignored.  The test runner sets it, because a result class is free to filter
  what its constructor gets.
* The unittest -v option is now counted, so that -vv means 3.
* The verbosity of regrtest is one less, so it is translated where the test
  runner is created: -v reports the test names, as before, and -vv reports
  also the examples.

Co-authored-by: Claude Opus 5 (1M context) <noreply@anthropic.com>
@serhiy-storchaka serhiy-storchaka added stdlib Standard Library Python modules in the Lib/ directory tests Tests in the Lib/test dir labels Aug 8, 2026
@read-the-docs-community

Copy link
Copy Markdown

Documentation build overview

📚 cpython-previews | 🛠️ Build #33972298 | 📁 Comparing 5698da1 against main (8ed1479)

  🔍 Preview build  

4 files changed
± library/doctest.html
± library/test.html
± library/unittest.html
± whatsnew/changelog.html

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

Labels

awaiting core review stdlib Standard Library Python modules in the Lib/ directory tests Tests in the Lib/test dir

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant