pytest-dev / pytest-dev/pytest
Warnings capture interferes with doctest
Nobody has claimed this yet.
- 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
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- 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