-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
Flowchart of pytest test session states and hooks #3261
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
Comments
Hi @Sup3rGeo, this would definitely be useful. I think we can use sphinx.ext.graphviz to generate the flowcharts. |
Nice! I would be willing to help with documentation (with graphviz) but I would just need someone who knows pytest execution flow to define in any way (text or in a tool like draw.io for instance) |
@Sup3rGeo thanks for the offer, we appreciate it! The hook order can be obtained by passing
Which can be used as a starting point, and in any case if there are any questions we would gladly answer them over a WIP PR. 😁 |
That is really great, can't believe all this time I didn't know it! I will try to come up with something then and create PR so you can review it. |
I was thinking about doing something like this: However I am not sure how to do this with graphviz. Mainly to represent the fact that some hooks are executed inside the context of other hooks. What do you think? |
I never used
I think it looks great! Also, perhaps you can write a script to generate the |
That is a great idea. From what I could see, not all the hooks are shown in the log file. Is that dependent on what pytest features the test is actually using? |
Not offhand, but IMHO don't worry about it too much: having the script in place generating a graph with the most common hooks and integrated with our release process is already a huge addition. We can always improve that bit later. 😁 |
So basically I wrote something to parse the log file and create a tree using The result:
The challenge was to not repeat hook calls and merge internal hooks from some calls that repeat but not exactly one after the other. But it seems to be working fine. I faced just one problem though - the last It also has the ability to generate dot graphs out of it, although the tree text representation is already very good and this graph is just unreadable: https://github.com/Sup3rGeo/pytest/blob/master/doc/pytesthooks.png |
So I ran a test again and same thing happened: One of the One possible solution is to consider the leading whitespaces to do the nesting. |
looks fabulous, can we add the scripts somewhere to the pytest repo so we can update in future (maybe it makes sense to put this into pluggy) |
@RonnyPfannschmidt nice to hear that! Merging repeated calls inside a parent is problematic because it does not capture the fact that, for instance, pytest__runtest_makereport runs three times, after setup, call, and teardown respectively. On the other hand, if we leave everything as is then typically you can have much useless repetition, for example many many pytest_plugin_registered and then the hooks from pytest_logstart to pytest_logfinish for every test case if you have many. So we might have to craft a test that will call just the right amount of hooks, so it looks good on the script, or we should perhaps think of other way. If we do it manually then it is easier to indicate that a certain hook sequence is repeated for every item collected, etc. |
we have a set of tools that use pluggy - all of those could benefit from a automated way to deal with hooks and their nesting ^^ this is a topic where playing and experimenting with collecting and displaying the data might give insights |
One idea is to manually indicate to the script a set of hooks that should appear only once, because this will vary from project to project. 😁 |
Just stumbled upon this presentation which has a similar tree view of the hooks (including annotations where there is repetitions): http://devork.be/talks/pluggy/pluggy.html Do you guys know him? Should we contact him and see if he might have a script for that already? |
Here's the video of the presentation for those interested: https://www.youtube.com/watch?v=zZsNPDfOoHU |
Hey, I'm afraid I just made that up from what I knew and eyeballing the code somewhat. I didn't attempt to make it complete or overly accurate as that was not the point of that slide. So you're miles ahead with the work done here! The script with some hints about which hook calls to collapse like @nicoddemus suggests seems like it should be pretty good. |
I updated the script to use a list of hooks to merge or to exclude from merging, but other than that I am out of ideas on the best way to do things like, for instance, indicate that We could define it to appear a number of times (in this case 3), or indicate that it should have one after each one of them explicitly. But because this is really code dependent, I cannot really think of a nice general way for solving this. |
Explicitly defining seems like a good solution to me. 👍 |
Hi @Sup3rGeo, any updates on this? The discussion gives an impression of a great journey towards a fine goal which suddenly stopped and went quiet ;) |
Hi! So this stopped so far because I couldn't figure out:
I tend to think the best approach would be to implement the last point mentioned as a first step (very clear) and then the resulting tree could be processed the way the user wants as a second step (not clear at all, so maybe should be out of scope of this). |
I got here because I was just thinking how cool this would be 😄 |
This is amazing, even unfinished. Doesnt have to be perfect/include EVERYTHING to be a big help. 10x! |
Could you please tell why pytest_plugin_registered is repeated under pytest_sessionstart. I checked pytestdebug.log and repeats same plugins' registeration. Please clarify. root |
@RonnyPfannschmidt I keep coming back to this task as it is the only documentation showing the hook call order or in general the hooks available.. Is there a reason why the hooks flow still isn't added to the documentation? I think that the script Sup3rGeo made is fine as-is. Another thing I am confused about. Why is pytest_runtest_makereport called before pytest_runtest_call? I would assume the report is made after the test is executed. |
Probably because nobody took the time to clean things up, perhaps annotate them a bit, and add them to the docs (and keep them updated once there are new hooks). PRs welcome! As another starting point, here is what I have in my company training slides:
It's called once in the setup phase and once in the call phase (and again in the teardown phase). The script probably falsely hides those as duplicates. Another example why this probably needs some more work with someone actually going through the hook output manually and making it human-readable without leaving crucial details out. |
That is surely MUCH better than what I had access to before. Trying to learn all these hooks and how it works is pretty tough when you don't have much to go with.
That's a good point. Hopefully somebody will find time to do it at some point. |
Looks like there are no actions we need to do, I suggest to close the issue. Pretty doc already exist. |
Specifically I think @storenth is reffering to the Hooks section located on that page: https://docs.pytest.org/en/8.0.x/reference/reference.html#hooks |
@storenth I disagree. I think the actual output that the I am not so strong in Sphinx yet but it should be possible to:
And then the task could be completed ..Right? |
As I said above, the raw output generated by that script has various issues which will probably confuse people reading the docs rather than help. |
Hi,
This is a documentation request to have a flowchart of all the pytest test session states (setup, conftest, collecting, run setup, run test, run teardown, make report, etc..) together with all the applicable hooks.
Something similar to the one on logging module documentation (I suppose that for pytest this would be way more complex):
https://docs.python.org/2/howto/logging.html#logging-flow
I surely read the page on the hooks but not knowing all the steps that are taken by pytest it becomes a bit hard to guess which one we should be using. This would really improve the understanding on how pytest works, especially for plugin developers.
The text was updated successfully, but these errors were encountered: