I’ve been curious about how pytest‘s assertion rewriting works. (As someone used to Abseil’s testing framework, the usage of bare assert
s was a shock.) I was happy to come across a Reddit post: “How does pytest work?”. Unfortunately, the site it points to, pythoninsight.com, seems to be mostly dead. Luckily, the wonderful Wayback Machine has a copy:
- Assertion rewriting in Pytest part 1: Why it’s needed
- Assertion rewriting in Pytest part 2: Simple workarounds
- Assertion rewriting in Pytest part 3: The AST
- Assertion rewriting in Pytest part 4: The implementation
Part 4 is the one that really gets deep into the guts, pointing to https://peps.python.org/pep-0302/ and walking through pytest’s implementation of rewriting. Looking at https://github.com/pytest-dev/pytest, we find AssertionRewritingHook as mentioned in the blog post.
I wonder what happened to pythoninsight.com. Is the “tim” posting there the same “Tim” as the one behind Timsort?
Leave a Reply