This is going to be a very personal one; grab some tissues I’m not sure why I’m sharing this here, but it just felt...
Powering up PyTest with Plugins
Vatsal ParekhPytest is one of very good testing frameworks out there, but what makes it great is the ability to customize it for needs of your test suit.
You can almost tweak almost every stage of the test run; from gathering/generating tests, running them to reporting the results.
To do all these tweaks, ‘pytest hooks’ are the entry point. Pytest has many well-specified hooks that help you define what you want pytest to do at those certain steps.
Some basic one of them are like below ones,
pytest_runtest_setup(item)
# called before pytest_runtest_call(item).
pytest_runtest_call(item)
# called to execute the test item.
pytest_runtest_teardown(item, nextitem)
# called after pytest_runtest_call.
To take an example, there is a hook called pytest_collection_modifyitems, when pytest goes for gathering the tests, it gives all the test found to this function you defined; you can simply iterate over each of them, inspect or modify whatever you want.
- Here is an example that uses this hook, and runs the test that strictly uses a specific fixture.
- Or to give a more specific example, we use it inside a marker to un-collect the test, if the product version is below some number; so you can simply mark your tests which are dependent on the feature of some version, like @pytest.mark.uncollecif(if version < x).
- You can also tweak your pytest to post the test run results to a specified server, maybe to generate better reports.
- Or even pick tests for next run from that result, and run the failed tests, if you keep your tests continuously running hoping something might get fixed in between.
There are a bunch of such markers we wrote, they may be our project specific, but you can look for your case if it matches 🙂
I should have used ‘hooks’ in the title rather than plugins, but pytest plugins are essentially a collection of hooks that match a certain purpose. Pytest has an extensive documentation around all these stuff, and a bunch of these plugins you can use, this was just a small glimpse 😉
Happy testing!