Skip to content

Warnings capture interferes with doctest #4732

Open
@alexshpilkin

Description

@alexshpilkin

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    plugin: doctestsrelated to the doctests builtin pluginplugin: warningsrelated to the warnings builtin plugintype: bugproblem that needs to be addressed

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions