pytest capsys:检查输出并得到报告? [英] pytest capsys: checking output AND getting it reported?

查看:61
本文介绍了pytest capsys:检查输出并得到报告?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Python 3.4.1,pytest 2.6.2.

Python 3.4.1, pytest 2.6.2.

当测试失败时,pytest 会定期报告测试打印到标准输出的内容.例如这段代码:

When a test fails, pytest will routinely report what was printed to stdout by the test. For instance this code:

def method_under_test():
    print("Hallo, Welt!")
    return 41

def test_result_only():
    result = method_under_test()
    assert result == 42

当作为 python -m pytest myfile.py 执行时,将报告此:

when executed as python -m pytest myfile.py, will report this:

================================== FAILURES ===================================
______________________________ test_result_only _______________________________

    def test_result_only():
        result = method_under_test()
>       assert result == 42
E       assert 41 == 42

pytestest.py:9: AssertionError
---------------------------- Captured stdout call -----------------------------
Hallo, Welt!
========================== 1 failed in 0.03 seconds ===========================

这是一个非常好的功能.但是当我使用 pytest 的内置 capsys 固定装置时,就像这样:

This is a very nice feature. But when I use pytest's built-in capsys fixture, like this:

def test_result_and_stdout(capsys):
    result = method_under_test()
    out, err = capsys.readouterr()
    assert out.startswith("Hello")
    assert result == 42

报告不再包含实际输出:

the report no longer contains the actual output:

================================== FAILURES ===================================
___________________________ test_result_and_stdout ____________________________

capsys = <_pytest.capture.CaptureFixture object at 0x000000000331FB70>

    def test_result_and_stdout(capsys):
        result = method_under_test()
        out, err = capsys.readouterr()
>       assert out.startswith("Hello")
E       assert <built-in method startswith of str object at 0x000000000330C3B0>('Hello')
E        +  where <built-in method startswith of str object at 0x000000000330C3B0> = 'Hallo, Welt!\n'.startswith

pytestest.py:14: AssertionError
========================== 1 failed in 0.03 seconds ===========================

我不确定这种行为是否符合规范;pytest 文档 介绍了 读取器:"测试功能完成后原始流将被恢复."

I am not sure whether this behavior is according to specification; the pytest documentation says about readouterr: "After the test function finishes the original streams will be restored."

我试过假设 capsys 是一个上下文管理器并且有在断言之前调用 capsys.__exit__() .这将是一个丑陋的解决方案,但至少是一个解决方案,如果它在我断言之前恢复了输出.然而,这只会产生

I have tried assuming capsys is a context manager and have called capsys.__exit__() just before the asserts. This would be an ugly solution, but at least a solution if it restored the output before my assertion. However, this only produces

AttributeError: 'CaptureFixture' object has no attribute '__exit__'

接下来我查看了 CaptureFixture 类源代码并发现了一个很有前途的方法 close(它调用一些 pop_outerr_to_orig() 方法),但是在我的测试中调用 capsys.close() 也没有帮助,完全没有明显的效果.

Next I looked into the CaptureFixture class source code and found a promising-looking method close (which calls some pop_outerr_to_orig() method), but calling capsys.close() in my test did not help either, it had no obvious effect at all.

我怎样才能让 pytest 在失败时报告我的输出在使用 capsys 的测试中?

How can I get pytest to report my outputs upon failure in a test using capsys?

推荐答案

您看到了正确的行为,当使用 capsys.readouterr() 时,您正在使用捕获的输出.因此,任何输出到 stdout 和 stderr 将不再显示在测试报告中.但是,您在此之后创建的但不使用的任何新输出仍将被报告,因此您只需再次将其写入输出流即可将完整输出返回到报告中:

You're seeing the correct behaviour, when using capsys.readouterr() you're consuming the captured output. Hence any output to stdout and stderr will no longer show up in the test report. But any new output which you create after this and do not consume will still be reported, so you can get the full output back in the report by simply writing it to the output streams once more:

def test_result_and_stdout(capsys):
    result = method_under_test()
    out, err = capsys.readouterr()
    sys.stdout.write(out)
    sys.stderr.write(err)
    assert out.startswith("Hello")
    assert result == 42

这篇关于pytest capsys:检查输出并得到报告?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆