Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Warnings capture interferes with doctest #4732

Open
alexshpilkin opened this issue Feb 6, 2019 · 1 comment
Open

Warnings capture interferes with doctest #4732

alexshpilkin opened this issue Feb 6, 2019 · 1 comment
Labels
plugin: doctests related to the doctests builtin plugin plugin: warnings related to the warnings builtin plugin type: bug problem that needs to be addressed

Comments

@alexshpilkin
Copy link

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
Copy link
Author

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

@Zac-HD Zac-HD added type: bug problem that needs to be addressed plugin: doctests related to the doctests builtin plugin plugin: warnings related to the warnings builtin plugin labels Feb 6, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
plugin: doctests related to the doctests builtin plugin plugin: warnings related to the warnings builtin plugin type: bug problem that needs to be addressed
Projects
None yet
Development

No branches or pull requests

2 participants