pytest-assume icon indicating copy to clipboard operation
pytest-assume copied to clipboard

Hook/callback to overwrite the failure summary message at the end of test run

Open ptrivedi2610 opened this issue 5 years ago • 8 comments

Hi @astraw38 ,

Can a hook/callback be implemented that allows the assume summary report at the end, to be printed in a format in which the test user needs.

ptrivedi2610 avatar Feb 04 '20 07:02 ptrivedi2610

@astraw38 Any help would be much appreciated.

ptrivedi2610 avatar Feb 21 '20 11:02 ptrivedi2610

Hi @ptrivedi2610 , it's doable, but I really don't have the time or bandwidth to be able to implement it right now. If you wanted to give a shot at it, I'd check out https://docs.pytest.org/en/latest/writing_plugins.html#declaring-new-hooks.

astraw38 avatar Feb 21 '20 13:02 astraw38

@astraw38 Yes, I would like to go ahead and implement. But I need to discuss about the approach. Does this mean overriding pytest_pyfunc_call and pointing to my own custom hook ?

ptrivedi2610 avatar Apr 23 '20 13:04 ptrivedi2610

@astraw38 Please guide me here.

ptrivedi2610 avatar Apr 24 '20 09:04 ptrivedi2610

  1. create a hooks.py file, containing the new hook as a stub
  2. use pytest_addhooks to register the new hook
  3. in pytest_assume, extract the current output into the new hook
  4. call the new hook in pytest_pyfunc_call.

At that point, you should be able to override the hook or add additional functionality in your own projects.

astraw38 avatar Apr 24 '20 12:04 astraw38

@astraw38

As discussed, I have started implementing hook pytest_assume_modify_summary_report() hook to give user control to print the summary in a customized way (similar approach to how I implemented pytest_assume_pass() and pytest_assume_fail())

Below is the approach I have taken

  1. Implement new hook pytest_assume_modify_summary_report(). If it is implemented by user, pytest-assume will use the content to dump will come from this hook, else it will use default content as used in pytest-assume itself.

Snippet

content = pytest._hook_assume_modify_summary_report( failed_assumptions=failed_assumptions) if not content: if getattr(pytest, "_showlocals"): content = "".join(x.longrepr() for x in failed_assumptions) else: content = "".join(x.repr() for x in failed_assumptions) else: #Plugin returns will come as list elements. content = "".join(x for x in content)

Note that the hook changes only the content coming from failed_assumptions variable. Rest of all are re-used from pytest_pyfunc_call(), as I believe the other stuff are necessary, and shouldn't be modified

Let me know your thoughts on the approach

ptrivedi2610 avatar May 28 '20 06:05 ptrivedi2610

@astraw38 Can you confirm above approach so that I can implement and send to you for review ?

ptrivedi2610 avatar Jun 02 '20 10:06 ptrivedi2610

I think it's about 80% of the way there, just a few notes.

  1. rename the hook to "pytest_assume_summary_report"
  2. extract the existing output to the new hook (just make sure it returns a string).

That way we don't need the 'if not content', and it's a bit clearer where the output is being generated.

I'll add these comments to the commit as well.

astraw38 avatar Jun 02 '20 12:06 astraw38