我正在使用Pytest编写测试。我有一个这样的装置:
@pytest.yield_fixture(autouse=True, scope='session')
def manage_tests():
print("Do stuff...")
do_stuff()
yield
Run Code Online (Sandbox Code Playgroud)
我在此处放置了一条打印语句,以便在运行测试时可以在控制台中看到它,以更好地了解程序的运行情况。但是我在控制台中看不到该文本,我猜想pytest会吞下它。有什么办法可以从固定装置打印?
与 pytest 3 一起使用此装置:
@pytest.fixture()
def tprint(request, capsys):
"""Fixture for printing info after test, not supressed by pytest stdout/stderr capture"""
lines = []
yield lines.append
with capsys.disabled():
for line in lines:
sys.stdout.write('\n{}'.format(line))
Run Code Online (Sandbox Code Playgroud)
Pytest不会吞下输出,默认情况下不会显示。要在控制台中查看输出,请尝试运行带有-s选项的测试,例如:
pytest -s <path_to_file>
Run Code Online (Sandbox Code Playgroud)