如何在pytest_sessionfinish()中捕获打印语句?

时间:2018-08-10 09:46:20

标签: python pytest

pytest不会捕获print()中的stdout语句(pytest_sessionfinish()输出)或在其中调用的代码。要获得相同或相似的行为(如位于测试,固定装置等中的print()语句)的最佳实践是什么?

# conftest.py

def pytest_sessionfinish(session, exitstatus):
    print('This wont be captured (like in tests) but output independent of pytest exit status (exitstatus).')
    if exitstatus == 0:
        print('stdout shall be captured/suppressed in case of overall test PASS.')
    else:
        print('stdout shall be output in case of overall test FAIL and any other error.')

0 个答案:

没有答案