我可以在哪个py.test标注中找到'item'和'report'数据?

Mar*_*hio 6 python pytest

pytest_runtest_makereport()获取两个参数,item和call.从item,我可以找到我为此测试创建的funcarg,并且从call中,我可以找到异常信息(如果有的话):

def pytest_runtest_makereport (item, call):
    my_funcarg = item.funcargs['name']
    my_funcarg.excinfo = call.excinfo
Run Code Online (Sandbox Code Playgroud)

不幸的是,填充了两个失败和跳过的excinfo.为了区分,我需要查看pytest_report_teststatus()的report参数:

def pytest_report_teststatus (report):
    if report.when == 'call':
        if report.failed:
            failed = True
        elif report.skipped:
            skipped = True
        else:
            passed = True
Run Code Online (Sandbox Code Playgroud)

这是很好的信息,但我无法将其与我为测试创建的funcarg相关联.我查看了report参数(一个TestReport报告),我找不到任何方法可以回到传递给pytest_runtest_makereport()的项目,或者我创建的funcarg.

我在哪里可以访问这两个?

hpk*_*k42 6

有一些未记录的非官方方法,钩子实现可以与其他钩子实现交互,例如对其结果进行后处理.在您的具体案例中,您可能会执行以下操作:

@pytest.mark.tryfirst
def pytest_runtest_makereport(item, call, __multicall__):
    rep = __multicall__.execute()
    # your code follows and you can use rep.passed etc.
    return rep
Run Code Online (Sandbox Code Playgroud)

笔记:

  • 钩子调用通常会调用多个钩子实现
  • "tryfirst"标记指示钩子调用以尽早调用您的实现
  • 多重呼叫参数表示正在进行的呼叫钩和可用于调用剩余钩实现,然后
    使用其结果进行进一步的处理
  • 你需要在这里返回"rep",因为你影响了"真正的"创造

线程 API很少真正,我怀疑可能有针对您的用例的解决方案,不需要它.

HTH,Holger