This talk is not about how to use pytest. pytest is a good project to explore metaprogramming techniques like introspection and code as data in the context of solving real world problems.
In this case: implementing a test framework.
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.
Algorithms, Code-Review, APIs, Use Cases
Domain Expertise:none
Python Skill Level:basic
Abstract as a tweet:Abridged metaprogramming classics - this episode: pytest. About the role of metaprogramming in the creation of a simple to use but powerful testing framework.
Public link to supporting material: