相关疑难解决方法(0)

pytest capsys:检查输出并报告?

Python 3.4.1,pytest 2.6.2.

当测试失败时,pytest将定期报告测试中打印到stdout的内容.例如这段代码:

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

def test_result_only():
    result = method_under_test()
    assert result == 42
Run Code Online (Sandbox Code Playgroud)

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

================================== 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 ===========================
Run Code Online (Sandbox Code Playgroud)

这是一个非常好的功能.但是当我使用pytest的内置capsys夹具时,如下所示:

def test_result_and_stdout(capsys):
    result = method_under_test()
    out, err = capsys.readouterr()
    assert out.startswith("Hello")
    assert result …
Run Code Online (Sandbox Code Playgroud)

python pytest

12
推荐指数
2
解决办法
6800
查看次数

标签 统计

pytest ×1

python ×1