Abridged metaprogramming classics - this episode: pytest
Oliver Bestwalter
If pytest seems like pure magic to you or you even don't know what a test framework does, this talk will hopefully give you some insights.
We will take a look at 3 pytest core features and see how they are implemented using metaprogramming techniques like using code as data, dynamic execution of arbitrary expressions and live object introspection. These are all big words, but in Python all this comes quite natural. Getting started with these things is not too hard and can be done in an incremental fashion.
To get to the essential ideas of how metaprogramming can help in creating a testing framework we will look at:
- Automatic test discovery and execution. What happens when I run
pytest path/to/my/tests
? - Selecting marked tests via expressions (
@pytest.mark
). How do I implement@pytest.mark.<arbitrarily_named_marker>
and runpytest -m
with an expression to select specific tests decorated with markers? - Automatic dependency injection (
@pytest.fixture
). How do I implement a fixture mechanism and what happens if I use a fixture in my test?
This is an introductory talk. No knowledge is expected of either pytest or metaprogramming. The functionality will be explained and built from the ground up for each feature. This is also a very code heavy talk, but It turns out that if you don't have to worry about the real world, you only need a few lines of code as Python itself does the heavy lifting :)
A more complete version of the code shown in the talk lives in the pico-pytest repository). it contains an installable, tested project that also implements only the bare functionality, but has automatic tests and makes use of type hinting (these are especially useful in programs that are very dynamic and make use of metaprogramming).
Disclaimer: no third party tools where harmed in the making of this talk: metaprogramming is inbuilt into Python and well supported by the language and its standard library.
Oliver Bestwalter
Affiliation: Avira
Oliver fell in love with open source in the 1990s and with Python in 2006. He creates and helps maintain test and automation tools helping developers and companies to produce better software more effectively.
In 2016 he joined the tox project and is now one of the maintainers. Since 2017 he has been spending up to 20% of his time at Avira working on tox and other open source projects.