我可以在哪个 py.test 标注中找到“项目"和“报告"数据? [英] In which py.test callout can I find both 'item' and 'report' data?

查看:22
本文介绍了我可以在哪个 py.test 标注中找到“项目"和“报告"数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

pytest_runtest_makereport() gets two arguments, item and call. From item, I can find the funcarg I created for this test, and from call, I can find the exception info (if any):

def pytest_runtest_makereport (item, call):
    my_funcarg = item.funcargs['name']
    my_funcarg.excinfo = call.excinfo

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

Unfortunately, excinfo is populated for both failures and for skips. To distinguish, I need to look at the report argument to pytest_report_teststatus():

def pytest_report_teststatus (report):
    if report.when == 'call':
        if report.failed:
            failed = True
        elif report.skipped:
            skipped = True
        else:
            passed = True

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

That's great info, but I can't correlate it to the funcarg I created for the test. I have looked at the report argument (a TestReport report), and I can't find any way to get back to the item passed to pytest_runtest_makereport(), or the funcarg I created.

我可以从哪里获得两者的访问权限?

Where can I get access to both?

推荐答案

有一些未公开的、有点非官方的方法,钩子实现可以用它与其他钩子实现交互,例如对它们的结果进行后处理.在您的具体情况下,您可能会执行以下操作:

There is a little undocumented somewhat unofficial method with which hook implementations can interact with other hook implementations, for example to post-process their result. In your concrete case you might do something like:

@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

注意事项:

  • 一个钩子调用通常会调用多个钩子实现
  • tryfirst"标记指示钩子调用提前调用您的实现
  • multicall 参数表示正在进行的挂钩调用,并且可以用于调用剩余的钩子实现然后
    将他们的结果用于进一步处理
  • 您需要在此处返回rep",因为您隐藏了真实"的创作
  • a hook call will usually call multiple hook implementations
  • the "tryfirst" marker instructs the hook call to have your implementation be invoked early
  • the multicall parameter represents the ongoing hook call and can be used to call the remaining hook implementations and then
    use their result for further processing
  • you need to return "rep" here because you shadow the "real" creation

multicall API 很少是真的,我怀疑您的用例可能有不需要它的解决方案.

The multicall API is very seldomly really and i suspect there could be solutions for your use case that don't require it.

HTH,霍尔格

这篇关于我可以在哪个 py.test 标注中找到“项目"和“报告"数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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