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

2024-09-26 22:41:52 发布

您现在位置:Python中文网/ 问答频道 /正文

pytest不捕获位于pytest_sessionfinish()或其中调用的代码中的print()语句(stdout输出)。在测试、fixture等中获得相同或类似行为的最佳实践是什么。?在

# 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.')

Tags: of代码intestoutputpyteststdoutbe

热门问题