tl;dr
Title |
Python Testing with pytest |
Author |
Brian Okken (@brianokken) |
Publication |
2017 (The Pragmatic Programmers) |
Summary |
How to use all the features of pytest for Python test automation – “simple, rapid, effective, and scalable.” |
Prerequisites |
Intermediate-level Python programming. |
Summary
Python Testing with pytest is the book on pytest*. Brian Okken covers all the ins and outs of the framework. The book is useful both as tutorial for learning pytest as well as a reference for specific framework features. It covers:
- Getting started with pytest
- Writing simple tests as functions
- Writing more interesting tests with assertions, exceptions, and parameters
- Using all the different execution options
- Writing fixtures to flexibly separate concerns and reuse code
- Using built-in fixtures like tmpdir, pytestconfig, and monkeypatch
- Using configuration files to control execution
- Integrating pytest with other tools like pdb, tox, and Jenkins
Appendices also cover:
- Using Python virtual environments
- Installing packages with pip
- An overview of popular plugins like pytest-xdist and pytest-cov
- Packaging and distributing Python packages
[* Update on 9/27/2018: Check out my review on another excellent pytest book, pytest Quick Start Guide by Bruno Oliveira.]
Praises
This book is a comprehensive guide to pytest. It thoroughly covers the framework’s features and gives pointers to more info elsewhere. Even though pytest has excellent online documentation, I still recommend this book to anyone who wants to become a pytest master. Online docs tend to be fragmented with each piece limited in scope, whereas books like this one are designed to be read progressively and orderly for maximal understanding of the material.
I love how this book is example-driven. Each section follows a simple yet powerful outline: idea → code → output → explanation. Having real code with real output truly cements the point of each mini-lesson. New topics are carefully unfolded so that they build upon previous topics, making the book read like a collection of tutorials. Examples at the end of every chapter challenge the readers to practice what they learn. The formatting of each section also looks great.
The extra info on related topics like pip and virtualenv is also a nice touch. Python pros probably don’t need it, but beginners might get stuck without it.
The rocket ship logo on the cover is also really cool!
Takeaways
pytest is one of the best functional test frameworks currently available in any language. It breaks the clunky xUnit mold, in which class structures were awkwardly superimposed over test cases as if one size fits all. Instead, pytest is lightweight and minimalist because it relies on functions and fixtures. Scope is much easier to manage, code is more reusable, and side effects can more easily be avoided. pytest has taken over Python testing because it is so Pythonic.
Brian’s concise writing style has also inspired me to be more direct in my own writing. I tend to be rather verbose out of my desire to be descriptive. However, fewer words often leave a more powerful impression. They also make the message easier to comprehend. Python is beloved for its concise expressiveness, and as a Pythonista, it would be fitting for me to adopt that trait into my English.
If I had a wish list for a second edition, I’d love to see more info about assertions and other plugins (namely pytest-bdd). I think an appendix with comparisons to other Python test frameworks would also be valuable.
A Warning
I ordered a physical copy of this book directly from Amazon (not a third-party seller). Unfortunately, that copy was missing all the introductory content: the table of contents, the acknowledgements, and the preface. The first page after the front cover was Chapter 1. Befuddled, I reached out to Brian Okken (who I personally met at PyCon 2018). We suspected that it was either a misprint or a bootleg copy. Either way, we sent the evidence to the publisher, and Amazon graciously exchanged my defective copy for the real deal. Please look out for this sort of problem if you purchase a printed copy of this book for yourself!
If you want to learn more about pytest, please read my article Python Testing 101: pytest.