In which py.test callout can I find both 'item' and 'report' data?

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-04 12:17:40

问题


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

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

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?


回答1:


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

Notes:

  • 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

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

HTH, Holger




回答2:


While hpk42's answer works, __multicall__ is being depreciated.

This achieves the same result though:

@pytest.hookimpl(hookwrapper=True)
def pytest_runtest_makereport(item, call):
    outcome = yield
    rep = outcome.get_result()
    setattr(item, "rep_" + rep.when, rep)
    return rep


来源:https://stackoverflow.com/questions/10754970/in-which-py-test-callout-can-i-find-both-item-and-report-data

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!