Preface
Preface
This PDF file contains pages extracted from Python Testing with pytest, Second
Edition, published by the Pragmatic Bookshelf. For more information or to purchase
a paperback or PDF copy, please visit http://www.pragprog.com.
Note: This extract contains some colored text (particularly in code listing). This
is available only in online versions of the books. The printed versions are black
and white. Pagination might vary between the online and printed versions; the
content is otherwise identical.
Copyright © 2022 The Pragmatic Programmers, LLC.
Brian Okken
All rights reserved. No part of this publication may be reproduced, stored in a retrieval system,
or transmitted, in any form, or by any means, electronic, mechanical, photocopying, recording,
or otherwise, without the prior consent of the publisher.
ISBN-13: 978-1-68050-860-4
Encoded using the finest acid-free high-entropy binary digits.
Book version: P1.0—February 2022
Preface
The use of Python is increasing not only in software development, but also
in fields such as data science, machine learning, data analysis, research sci-
ence, finance, and just about all other industries. The growth of Python in
many critical fields also comes with the desire to properly, effectively, and
efficiently put software tests in place to make sure the programs run correctly
and produce the correct results. In addition, more and more software projects
are embracing continuous integration and including an automated testing
phase. There is still a place for exploratory manual testing—but thorough
manual testing of increasingly complex projects is infeasible. Teams need to
be able to trust the tests being run by the continuous integration servers to
tell them if they can trust their software enough to release it.
Enter pytest. pytest is a robust Python testing tool that can be used for all
types and levels of software testing. pytest can be used by development teams,
quality assurance teams, independent testing groups, and individuals prac-
ticing test-driven development, for both commercial and open-source projects.
In fact, projects all over the Internet have switched from unittest or nose to
pytest, including Mozilla and Dropbox. Why? Because pytest offers powerful
features such as assert rewriting, a third-party plugin model, and a powerful
yet simple fixture model that is unmatched in any other testing framework.
Why pytest?
pytest is a software testing framework, which means pytest is a command-
line tool that automatically finds tests you’ve written, runs the tests, and
reports the results. It has a library of goodies that you can use in your tests
to help you test more effectively. It can be extended by writing plugins or
installing third-party plugins. And it integrates easily with other tools like
continuous integration and web automation.
Here are a few of the reasons pytest stands out above many other testing
frameworks:
• You use assert in tests for verifications, not things like self.assertEqual() or
self.assertLessThan(). Just assert.
• You can use pytest to run tests written for unittest or nose.
Cards has a command-line interface (CLI). The CLI interacts with the rest of
the code through an application programming interface (API). The API is the
interface where you’ll direct most of your testing. The API interacts with a
database control layer, which interacts with a document database, TinyDB.
In Part II, Working with Projects, on page ?, you’ll look at some real-world
issues around testing projects, as well as explore more of the power of pytest.
You’ll start by exploring a simple testing strategy process and applying it to
the Cards project. You’ll take a look at configuration files and all of the other
non-test files involved in testing projects. You’ll use coverage analysis to look
at where our testing holes are with respect to Cards, and use mocking to help
test the user interface and fill in some coverage gaps. Really all testing involves
some debugging of both code and tests, so you’ll take a look at some of the
great features pytest has to help us debug test failures. Many projects utilize
continuous integration (CI). Tox is a popular framework to simulate a local
CI system. You’ll look at using pytest with tox and with hosted CI systems.
Part II also includes a look at the Python search path. The Cards project is
an installable Python package; however, not all testing projects involve
installed packages. This chapter in Part II looks at how you can tell pytest to
find your source code.
In Part III, Booster Rockets, on page ?, you’ll take your tests to the next
level. You’ll learn how to use third-party plugins to extend the capabilities of
pytest and learn how to build your own plugins. You’ll also learn advanced
parametrization techniques that build on what you learned in Part I.
pip
You should use pip to install pytest and pytest plugins. If you want a
refresher on pip, check out Appendix 2, pip, on page ?.
A command line
I wrote this book and captured the example output using zsh on a Mac
laptop. However, the only commands I use in zsh are cd to go to a specific
directory, and pytest, of course. Because cd exists in Windows cmd.exe and
all Unix shells that I know of, all examples should be runnable on what-
ever terminal-like application you choose to use.
That’s it, really. You don’t need to be a programming expert to start writing
automated software tests with pytest.
There have also been updates to Python that are reflected in the book:
Also, since publication of the first edition, I have taught many, many people
about pytest, and I think I’ve learned how to be a better teacher. The second
edition not only expands on what is covered in the first edition—it grew from
7 to 16 chapters!—but also it presents the material in what I think is a more
gradual, digestible manner.
• A discussion of test strategy. Feedback from the first edition was that the
book was great for the mechanics of how to use pytest, but the “What test
do I write?” information was a bit lacking. The new Chapter 7, Strategy,
on page ? is a push in the right direction of what tests to write. A com-
plete treatment of test strategy would be a book in itself; however, this
chapter will get you started.
• Information about the Python search path. A lot of readers reached out to
me asking about how to get their tests to see their test code, and the first
edition didn’t cover it. The project in this book, Cards, doesn’t have that
problem because it’s an installed Python package. However, lots of user
projects are applications or scripts or lots of other things that are not
installed packages. This chapter offers a focused look at the problem and
provides some solutions.
Finally, the example project changed. The first edition used a project called
Tasks to illustrate how to use pytest. Now it’s called Cards. Here’s why:
• It’s easier to say out loud. (Try it. Say “tasks” three times, then “cards”
three times. Right?)
• The new project itself is different because it uses Typer instead of Click
for command-line functionality. Typer code is easier to read.
• The project also uses Rich for formatting the output. Rich didn’t exist
(neither did Typer) when the first edition was written.
The code examples have also been simplified. The directory structure of the
first edition code examples followed a progression of a possible test directory
within a project, with most of the project removed. Seriously, I think it made
sense to me at the time. In this edition, there is a project in its own directory,
cards_proj, with no tests. Then, each of the chapters have test code (if appropri-
ate) that either work on the one project or on some local code. Trust me, I
think you’ll agree that it’s way easier to follow along now.
1. https://pythontest.com/pytest-book
2. https://pragprog.com/titles/bopytest2
that notes any updates you need to be aware of for future versions of pytest
and this book.
The source code for the Cards project, as well as for all of the tests shown in
this book, is available through a link on the book’s web page.3 You don’t need
to download the source code to understand the test code; the test code is
presented in usable form in the examples. But to follow along with the Cards
project, or to adapt the testing examples to test your own project (more power
to you!), you must go to the book’s web page to download the project.
To learn more about software testing in Python, you can also check out
pythontest.com4 and testandcode.com,5 a blog and podcast I run that discuss
the topic.
I’ve been programming for decades, and nothing has made me love writing
test code as much as pytest. I hope you learn a lot from this book, and I hope
you’ll end up loving test code as much as I do.
3. https://pragprog.com/titles/bopytest2/source_code
4. https://pythontest.com
5. https://testandcode.com