posted: July 10, 2017
tl;dr: A great introductory guidebook to get you up and running with pytest...
It might be considered unfair to review a book that is still in beta: I willingly bought the Beta 1.0 release about a month ago, and the book has been updated three times since then, mostly to add more material. So I’m not going to issue any dings for typos, as the book is still under review; I found a few minor ones, which I will submit as requested by the author Brian Okken, host of the Test and Code podcast and co-host of Python Bytes. But even in beta form, I found Python Testing with pytest to be an eminently usable introductory guidebook to the pytest testing framework. It is already paying dividends for me at my company.
Pytest appears to be picking up momentum in the Python community. In my opinion it is an easier to use, more straightforward, and more powerful framework than unittest, which is part of the Python standard library. Because unittest is in the standard library it will be in use for a long time, but pytest, in large part because it uses the standard assert operator and can be written in a purely functional style (while also supporting classes), is easier to read and write. Simplicity is one of the joys of using Python, and pytest makes it simpler to write and run tests than unittest. As with any framework there is magic happening behind the scenes, and certain coding patterns that must be followed when writing pytest tests. Hence the need for a book like Python Testing with pytest.
Brian Okken has many years of experience as a test-savvy lead software engineer, using pytest and other frameworks (unittest, nose), and his passion for pytest shines through in the book. He does a great job of starting at absolute ground zero with pytest, to gently bring the reader along and up the learning curve. He shows how to set up project and test environments, how to install pytest and organize files, and then starts with some very simple tests, gradually exposing the reader to the full power of pytest. By the end of the book, the reader has been exposed to all the main features of pytest, and should hopefully be at a point where the project documentation and online sources like StackOverflow can be searched to answer additional questions.
Especially useful is chapter 7, “Using pytest with Other Tools”, which covers (among other topics) mocking parts of the system under test, measuring test coverage, and integrating with the Jenkins Continuous Integration tool. Jenkins is a widely-used open source project, and there were some good tips on how to run pytest inside of a Jenkins project to produce informative output; we have put some of this to use already at my company. While most of the examples throughout the book are easy to follow and run, the most complex example in the book appears in this chapter in the mock section, because it uses the Click command line interface. As Okken admits, the code for the Click-based CLI "is not obvious”, and combining Click with mock produced some code that was hard to follow. But pretty much all the other examples in the book were chosen to be as easy as possible for the particular teaching point that Okken was making, with nothing extraneous. Mocking is a complex enough topic that perhaps it deserves its own chapter.
So even though Python Testing with pytest is still in beta, it is very helpful and usable today. I expect that Brian Okken will continue to refine it and add content to it even after it achieves its first official release. Hopefully Python Testing with pytest will achieve the success it deserves, and help to further spread the use of pytest throughout the Python community.