pytest-assume
pytest-assume copied to clipboard
Hook/callback to overwrite the failure summary message at the end of test run
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.
@astraw38 Any help would be much appreciated.
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 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 ?
@astraw38 Please guide me here.
- create a hooks.py file, containing the new hook as a stub
- use
pytest_addhooks
to register the new hook - in pytest_assume, extract the current output into the new hook
- 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
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
- 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
@astraw38 Can you confirm above approach so that I can implement and send to you for review ?
I think it's about 80% of the way there, just a few notes.
- rename the hook to "pytest_assume_summary_report"
- 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.