如何在收集过程中在测试功能之外的pytest中清除捕获的stdout / stderr?

问题描述 投票:0回答:1

示例test.py

print(1)
# clear stdout here
print(2)
1/0 # arbitrary error

致电pytest:

$ python3 -m pytest test.py 
============================= test session starts ==============================
platform linux -- Python 3.6.7, pytest-4.5.0, py-1.8.0, pluggy-0.11.0
rootdir: /home/user/Documents
collected 0 items / 1 errors                                                   

==================================== ERRORS ====================================
___________________________ ERROR collecting test.py ___________________________
test.py:4: in <module>
    1/0
E   ZeroDivisionError: division by zero
------------------------------- Captured stdout --------------------------------
1
2
!!!!!!!!!!!!!!!!!!! Interrupted: 1 errors during collection !!!!!!!!!!!!!!!!!!!!
=========================== 1 error in 0.07 seconds ============================

您可以看到,同时捕获了12。我想清除两者之间的标准输出,因此仅显示2。在测试功能内部,我可以使用capsys固定装置,按here所述访问捕获的标准输出。在测试功能以外的情况下,我可以使用什么?

python pytest stdout
1个回答
0
投票

[当您使用capsys.readouterr时,它将已经捕获所有输出,并且在下一次调用时,仅从上次运行readouterr起,您将获得stdout / stderr。

© www.soinside.com 2019 - 2024. All rights reserved.