A couple of months ago, Brian Okken asked me if I would be interested in reading his book, Python Testing with pytest. I have been interested in learning more about the pytest package for a while, so I agreed to take a look. I also liked that the publisher was The Pragmatic Programmers, which I’ve had good experience with in the past. We will start with a quick review and then dive into the play-by-play.
- Why I picked it up: The author of the book asked me to read his book
- Why I finished it: I mostly skimmed the book to see how it was written and to check out the examples
- Iâ€™d give it to: Anyone who is interested in testing in Python and especially in the pytest package
You can get this as a physical soft cover, Kindle on Amazon or various other eBook formats via Pragmatic Programming’s website.
This book has 7 chapters, 5 appendices and is 222 pages long.
This book jumps right in by starting off with an example in chapter 1. I actually found this a bit jarring as usually chapters have an introduction at the beginning that goes over what the chapter will be about. But chapter one just jumps right in with an example of a test in Python. It’s not bad, just different. This chapter explains how to get started using pytest and covers some of the common command line options you can pass to pytest.
Chapter two goes into writing test functions with pytest. It also talks about how pytest uses Python’s assert keyword rather than assert methods like Python’s unittest library does. I found that to be an appealing feature of pytest. You will also learn how to skip tests and how to mark tests that we expect will fail as well as a few other things.
If you’ve wondered about fixtures in pytest, then you will be excited to know that this book has two chapters on the topic; specifically chapters three and four. These chapters cover a lot of material, so I will just mention the highlights. You will learn about creating fixtures for setup and teardown, how to trace fixture execution, fixture scope, parameterized fixtures and builtin fixtures.
Chapter five is about how to add plugins to pytest. You will also learn how to write your own plugins, how to install them and how to test your plugins. You will also get a good foundation in how to use conftest.py in this chapter.
In chapter six, we learn all about configuring pytest. The big topics covered in this chapter deal with pytest.ini, conftest.py and __init__.py as well as what you might use setup.cfg for. There are a lot of interesting topics in this chapter as well such as registering markers or changing test discovery locations. I encourage you to take a look at the book’s table of contents to learn more though!
Finally in chapter 7 (the last chapter), we learn about using pytest with other testing tools. In this case, the book covers, pdb, coverage.py, mock, tox, Jenkins and even unittest.
The rest of the book is a series of five appendices and an index. The appendices cover virtual environments, pip, a plugin sampler, packaging / distributing Python projects and xUnit fixtures.
I thought this book was well written and stayed on topic well. The examples are short and to the point. I am looking forward to diving more deeply into the book when I want to use the pytest package for my own code. I would recommend this book to anyone who is interested in the pytest package.
Python Testing with pytest
by Brian Okken
Other Book Reviews
- Module Programming with Python by Erik Westra
- Python Playground â€“ Geeky Projects for the Curious Programmer by Mahesh Venkitachalam
- IPython Notebook Essentials by L. Felipe Martins
- Creating Apps in Kivy by Dusty Phillips
- Kivy â€“ Interactive Applications in Python by Roberto Ulloa
- Instant Flask Web Development by Ron DuPlain
- Real Python by Fletcher Heisler
- Python 3 Object Oriented Programming by Dusty Phillips