In which py.test box can I find the data "item" and "report"?

pytest_runtest_makereport () receives two arguments, an element and a call. From the element, I can find the funcarg that I created for this test, and from the call, I can find the exception information (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 passes. To distinguish, I need to look at the report argument at 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 

This is great information, but I can't match it with the funcarg I created for the test. I looked at the report argument (TestReport report) and I cannot find a way to return to the element passed to pytest_runtest_makereport () or the funcarg I created.

Where can I access both?

+4
source share
2 answers

There is a small, undocumented, somewhat unofficial method by which hook implementations can interact with other hook implementations, for example, for subsequent processing of their result. In your specific case, you can 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:

  • The hook call is usually called by several hook implementations.
  • the "tryfirst" marker points to a hook call so that your implementation is called earlier.
  • multicall parameter represents the current call call and can use it to call the remaining hook implementations, and then use their result for further processing.
  • you need to return "rep" here because you are obscuring the "real" creation.

The multicall API is very rare, and I suspect there may be solutions for your use case that do not require it.

HTH, Holger

+7
source

While hpk42 is responding, __multicall__ depreciating.

This gives the same result:

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

Source: https://habr.com/ru/post/984875/


All Articles