我在--doctest-modules上运行pytest,没有其他选择。
我有一个失败的doctest测试,我试图通过向底层代码添加print()语句来调试这个问题。
对于我的常规(非doctest)测试,捕获输出的工作和显示与预期一样,但是doctest测试没有在我的pytest输出中显示stdout,尽管我已经在其中执行了print()语句。
我可以将我的doctest重写为一个非doctest测试,以便让这些print()语句流转,但这听起来像是一件非常曲折的事情。
有没有办法告诉pytest,我也想让它捕获doctests的输出?
$ python --version
Python 3.7.2
$ pytest --version
This is pytest version 3.10.1, imported from /home/gene/.pythonz/pythons/CPython-3.7.2/lib/python3.7/site-packages/pytest.py
setuptools registered plugins:
pytest-xdist-1.26.1 at /home/gene/.pythonz/pythons/CPython-3.7.2/lib/python3.7/site-packages/xdist/plugin.py
pytest-xdist-1.26.1 at /home/gene/.pythonz/pythons/CPython-3.7.2/lib/python3.7/site-packages/xdist/looponfail.py
pytest-pythonpath-0.7.3 at /home/gene/.pythonz/pythons/CPython-3.7.2/lib/python3.7/site-packages/pytest_pythonpath.py
pytest-mock-1.10.0 at /home/gene/.pythonz/pythons/CPython-3.7.2/lib/python3.7/site-packages/pytest_mock.py
pytest-forked-1.0.1 at /home/gene/.pythonz/pythons/CPython-3.7.2/lib/python3.7/site-packages/pytest_forked/__init__.py
hypothesis-4.4.1 at /home/gene/.pythonz/pythons/CPython-3.7.2/lib/python3.7/site-packages/hypothesis/extra/pytestplugin.py
flaky-3.5.3 at /home/gene/.pythonz/pythons/CPython-3.7.2/lib/python3.7/site-packages/flaky/flaky_pytest_plugin.py发布于 2019-07-10 05:04:42
将我的评论转化为答案:最简单的方法是打印到stderr,因为doctest只捕获stdout进行比较。示例:
import sys
def greet(who):
"""Greet someone.
>>> greet('world')
'Hello world'
>>> greet('fizz')
'Hello fizz'
>>> greet('buzz')
'Hello buzz'
"""
print('input:', who, file=sys.stderr)
return f'Hello {who}'运行测试:
$ pytest --doctest-modules -sv
======================================= test session starts ========================================
...
collected 1 item
spam.py::spam.greet input: world
input: fizz
input: buzz
PASSED
===================================== 1 passed in 0.03 seconds =====================================https://stackoverflow.com/questions/56957335
复制相似问题