pytest icon indicating copy to clipboard operation
pytest copied to clipboard

Warnings capture interferes with doctest

Open alexshpilkin opened this issue 7 years ago • 4 comments

The following approach to testing warnings works when using vanilla doctest, but fails under pytest:

>>> import sys, warnings
>>> sys.stderr = sys.stdout # for doctest
>>> class Foo:
...     def __init__(self):
...         warnings.warn("Testing", UserWarning)
...
>>> foo = Foo() # doctest:+ELLIPSIS
/...: UserWarning: Testing
  ...

What this does is redirect the warning output (which usually goes to stderr) to stdout, where it can and will be captured by doctest. Yes, it’s a hack, but I find it very surprising that it just suddenly ceases to work when run under pytest. (I understand the reason it does so, but I believe there should be a way around it.)

I’m willing to program a solution if I can think of one, but so far I can’t.

System info:

$ uname -srvmo
Linux 4.20.4-arch1-1-ARCH #1 SMP PREEMPT Wed Jan 23 00:12:22 UTC 2019 x86_64 GNU/Linux
$ pipenv run python --version
Python 3.7.2
$ pipenv run pip list
Package        Version
-------------- -------
atomicwrites   1.3.0  
attrs          18.2.0 
more-itertools 5.0.0  
pip            19.0.1 
pluggy         0.8.1  
py             1.7.0  
pytest         4.2.0  
setuptools     40.8.0 
six            1.12.0 
wheel          0.32.3

alexshpilkin avatar Feb 06 '19 16:02 alexshpilkin

Here’s a gist that contains everything that’s needed to reproduce: https://gist.github.com/alexshpilkin/9283951ef0d511cc018547f13a557b4a.

alexshpilkin avatar Feb 06 '19 16:02 alexshpilkin

Hi! Triaging older bug reports — I'd like to nudge this one toward closure unless it's still reproducible on a current pytest.

  • Filed 2019-02-06, no activity since 2019-02-06 (7+ years; 1 prior comments).

If you can still reproduce on the latest pytest release with a minimal example (versions, exact invocation, the full output), I'm happy to dig further. Otherwise it might be a candidate to close.

(Disclosure: I drafted this comment with help from Claude Code while triaging stale issues; the activity dates and any source-path references above were verified directly against current main.)

jbbqqf avatar May 09 '26 14:05 jbbqqf

I get the impression that @jbbqqf’s comment above isn’t endorsed by the maintainers, but just in case: the issue is still reproducible on Python 3.13.9 with its bundled doctest vs pytest 8.4.2, with the exception that the /...: wildcard (matching an arbitrary path and line number) should be changed to <...: or <...>:...: (matching something like <doctest test_doc.txt[2]>:3:), because the format of the source location for both stdlib doctest and pytest in doctest mode has apparently changed.

alexshpilkin avatar May 09 '26 16:05 alexshpilkin

I investigated this interactively with AI assistance and reproduced the output mismatch. This is a compatibility observation, not a proposed fix or a request to close the issue.

Tested on Windows, CPython 3.11.7, pytest 9.2.0.dev419+g082ed70e3, checkout 082ed70e314ee8e1ab117763d17fe801648f7508. This is the checkout I executed, not a claim about the latest main or every supported Python version. No custom warning handler/copying plugin was loaded.

Minimal test_warning_output.txt:

>>> import sys, warnings
>>> sys.stderr = sys.stdout
>>> warnings.warn("Testing", UserWarning) # doctest: +ELLIPSIS
<...>:...: UserWarning: Testing
  ...

For a neighboring control, make test_warning_silent.txt containing the same first three lines but no expected output. This control is deliberately constructed to expose the compatibility trade-off; it is not a second example from the original report.

With a pytest.ini containing only [pytest], and PYTEST_DISABLE_PLUGIN_AUTOLOAD=1 set, I executed:

python -m doctest test_warning_output.txt
python -m pytest -c pytest.ini --confcutdir=. -q test_warning_output.txt
python -m pytest -c pytest.ini --confcutdir=. -q -p no:warnings test_warning_output.txt

I repeated these commands with test_warning_silent.txt:

Example stdlib doctest pytest default pytest -p no:warnings
Expects warning text pass fail (Got nothing) pass
Same redirection, expects no output fail pass fail (Expected nothing)

The routing explains the difference: pytest's runtest warning context enters warnings.catch_warnings(record=record) with recording enabled. Python records the warning instead of emitting text to stderr, so redirecting stderr cannot deliver that warning to doctest's stdout buffer.

The practical constraint is that sys.stderr is sys.stdout alone does not distinguish these two expectations. Unconditionally turning off warning recording for doctests flips which example passes, rather than preserving both behaviors.

Would documenting this interaction in the doctest guide, with a warning-event assertion example (e.g. pytest.warns) and a link to the existing -p no:warnings escape hatch for rendered-output checks, be the preferred contribution here? I have not found a backward-compatible automatic routing change and am not presenting one as a fix.

Cemilcanoz avatar Oct 27 '26 22:10 Cemilcanoz