Pytest:是否有可能从pytest_runtest_protocol获取错误消息

时间:2019-09-10 16:31:36

标签: pytest

使用conftest.py钩子可以捕获test case name及其status,但是可以使用error message捕获pytest_runtest_protocol

我从pytest文档中找到了以下代码段:

def pytest_runtest_protocol(item, log=True, nextitem=None):
    # Provide's test case execution details
    reports = runtestprotocol(item, nextitem=nextitem)
    for report in reports:
        if report.when == 'call':
            print('\n%s --- %s' % (item.name, report.outcome))
    return True

文档中提到pytest_runtest_protocol也包含异常详细信息

1 个答案:

答案 0 :(得分:0)

发现经过两次搜索后,我们无法使用pytest_runtest_protocol()收到错误消息:

@pytest.hookimpl(tryfirst=True, hookwrapper=True)
def pytest_runtest_makereport(item, call):
    # execute all other hooks to obtain the report object
    outcome = yield
    rep = outcome.get_result()

    if rep.when == "call" and rep.failed:
        # Error message if failed
        print(rep.longreprtext)

Reference

记录了我对其他人的更改here