内部 fixture capsys
读取并返回到目前为止捕获的输出,重置内部缓冲区。
- 返回 将捕获的内容作为具有out和err 字符串属性的命名元组。
def test_output(capsys): print("hello") captured = capsys.readouterr() assert captured.out == "hello\n" # 也可以 capsys.readouterr().out capsys.readouterr().err
在with
暂时禁用捕捉
- 要在测试中暂时禁用捕获,两者capsys 都有capfd一个disabled()可以用作上下文管理器的方法,在with块内禁用捕获:
def test_disabling_capturing(capsys): print("this output is captured") with capsys.disabled(): print("output not captured, going directly to sys.stdout") print("this output is also captured")