-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
Add pytest_before_assert hook. #4047
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add pytest_before_assert hook. #4047
Conversation
21d056a
to
98dd9b9
Compare
You're right, I should have checked #3479 first. If the other feature will take much longer, we could consider merging this first as a basic version without parameters being passed to the hook. We'd have to change my before_hook to an assertion pass hook for that. Otherwise I'm ok with closing this, it's my fault for not checking the other PR. |
Thanks for the ping @nicoddemus. @robertschweizer, sounds interesting but I think I still don't understand 100% the hook. Can you give a more concrete example of how would you use the hook? Also how to use the inspect command you mentioned to get information? A small example would be ideal. |
Thanks for still considering this. I'll try and give a full example. For test documentation, we'd like to collect all the assertions in our test code. In the tests where we use this, most assertions compare against explicit values like string literals. When the assertion passes, it is then completely sufficient to print the original code, without information about the actual operand values. We started with writing our on assertion functions, probably similar to your first solution of #3457. They just used Setting this conftest.py: import inspect
def pytest_before_assert():
expr = inspect.stack()[7].code_context[0].strip()
print(expr) This test code: def test_some_code():
encountered = get_software_return_value()
assert encountered == "expected string" prints this output:
The stack depth of 7 is a bit ugly, but I'm not sure there's a better solution of printing the calling code. |
Reminded me of pytest-pdb which has some methods to find the test etc, and you could use this method to get to the actual |
This hook will run before every assert, in contrast to the pytest_assertrepr_compare hook which only runs for failing asserts. As of now, no parameters are passed to the hook. We use this hook to print the assertion code of passed assertions to collect test evidence. This is done using inspect:: inspect.stack()[7].code_context[0].strip() Signed-off-by: Schweizer, Robert <[email protected]>
98dd9b9
to
df0a559
Compare
Codecov Report
@@ Coverage Diff @@
## features #4047 +/- ##
============================================
+ Coverage 95.79% 95.82% +0.02%
============================================
Files 113 113
Lines 25235 25250 +15
Branches 2495 2496 +1
============================================
+ Hits 24175 24195 +20
+ Misses 741 739 -2
+ Partials 319 316 -3
Continue to review full report at Codecov.
|
Since the other PR has been stuck for a while, what do you think about merging this one? |
Thanks for the suggestion! That solution is also pretty dependent on Pytest implementation details, and I can't get it to work properly with the current versions. I'm thinking about relying only on the def pytest_before_assert():
stack = inspect.stack()
for index, frame_info in enumerate(stack):
if frame_info.function == "pytest_pyfunc_call":
print(stack[index - 1].code_context[0]) |
Hi @robertschweizer, It has been a long time since it has last seen activity, plus we have made sweeping changes on master to drop Python 2.7 and 3.4 support, so this PR has some conflicts which require attention. In order to clear up our queue and let us focus on the active PRs, I'm closing this PR for now. Please don't consider this a rejection of your PR, we just want to get this out of sight until you have the time to tackle this again. If you get around to work on this in the future, please don't hesitate in re-opening this! Thanks for your work, the team definitely appreciates it! |
This hook will run before every assert, in contrast to the
pytest_assertrepr_compare hook which only runs for failing asserts.
As of now, no parameters are passed to the hook.
We use this hook to print the assertion code of passed assertions to
collect test evidence. This is done using inspect::
Signed-off-by: Schweizer, Robert [email protected]
Thanks for submitting a PR, your contribution is really appreciated!
Here's a quick checklist that should be present in PRs (you can delete this text from the final description, this is
just a guideline):
changelog
folder, with a name like<ISSUE NUMBER>.<TYPE>.rst
. See changelog/README.rst for details.master
branch for bug fixes, documentation updates and trivial changes.features
branch for new features and removals/deprecations.Unless your change is trivial or a small documentation fix (e.g., a typo or reword of a small section) please:
AUTHORS
in alphabetical order;