pytest-dev / pytest-dev/pytest

Warnings capture interferes with doctest

Open
#4,732 3 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

plugin: doctests plugin: warnings type: bug
Dominant language
Python
Stars
14.5k
Forks
3.4k
Avg merge
2d 9h
Merged PRs (30d)
35

Description

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

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reproducing the supplied doctest and warning-capture example under pytest with the reported Python and pytest versions. Then trace how pytest handles stderr, stdout, warnings, and doctest capture; done means the warning output is captured by doctest under pytest without regressing normal pytest capture behavior.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
testing-qa
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Quiet
Clarity
Needs clarification
Newbie friendliness
42/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.