Book Review: Python testing with Pytest

Review

Python testing with Pytest book cover

Title: Python testing with Pytest, 2nd Edition

Author(s): Brian Okken

I've been a long time listener of the Python Bytes podcast (paused it at the moment because I do less Python), so I was very interested in reading Brian's pytest book.

Containing more than 250 pages, it covers a wide array of topics around testing, and not only specific to the pytest framework. The author provides a small project with which to iterate adding all kind of tests and configurations: unit testing, integration testing, coverage, tox (including how to use it from Github Actions), debugging via pdb, virtual environments, how to install pytest plugins, ...

Having a chapter dedicated to configuration is really good, helps a lot to have all the files and formats you can configure pytest with in a single place, so you can compare and decide which one suits you best. And during our read, we will find that it is full of good practices and tips, like the author’s method of feature prioritization, based on 5 factors, to decide what to test first or to test more in depth, or how to structure your code, or advices on how and what to test. Everything using clear explanations and simple yet effective examples.

If I had to mention something that could be improved, I can only think about the mocking chapter. Considering that mock testing is a quite important pillar, chapter 10 feels a bit short. It doesn't explains too much the topic, at least compared with other chapters where we get a detailed introduction of what we will be using. And the example of mocking is a bit uncommon, albeit understandable in the sense that in the sample code the API layer is really thin. I would have prioritized testing the business layer (API layer here) before the presentation layer (CLI here), as that would contain all business logic and often use other services and objects, but I get that the example project is better suited for CLI unit tests with a mocked API. I advise to also read realpython.com's tutorial about the mock library if you want a deep dive into mocking in Python.

I’ve used pytest for a few years and yet I learned some new tricks and details here and there, plus I reinforced my knowledge of other areas like parametrization and fixtures. I really liked the content and I prefer using pytest over unittest, so I totally recommended this book to anyone interested in testing in Python.

Tags: Books Development Python Reviews Testing

Book Review: Python testing with Pytest article, written by Kartones. Published on