Python#
pytest#
framework#
Test applications, packages, and libraries large and small with pytest, Python's most powerful testing framework. pytest helps you write tests quickly and keep them readable and maintainable. In this fully revised edition, explore pytest's superpowers - simple asserts, fixtures, parametrization, markers, and plugins - while creating simple tests and test suites against a small database application. Using a robust yet simple fixture model, it's just as easy to write small tests with pytest as it is to scale up to complex functional testing. This book shows you how.
pytest is undeniably the best choice for testing Python projects. It's a full-featured, flexible, and extensible testing framework. pytest's fixture model allows you to share test data and setup procedures across multiple layers of tests. The pytest framework gives you powerful features such as assert rewriting, parametrization, markers, plugins, parallel test execution, and clear test failure reporting - with no boilerplate code.
With simple step-by-step instructions and sample code, this book gets you up to speed quickly on this easy-to-learn yet powerful tool. Write short, maintainable tests that elegantly express what you're testing. Speed up test times by distributing tests across multiple processors and running tests in parallel. Use Python's builtin assert statements instead of awkward assert helper functions to make your tests more readable. Move setup code out of tests and into fixtures to separate setup failures from test failures. Test error conditions and corner cases with expected exception testing, and use one test to run many test cases with parameterized testing. Extend pytest with plugins, connect it to continuous integration systems, and use it in tandem with tox, mock, coverage, and even existing unittest tests.
Write simple, maintainable tests quickly with pytest.
What You Need:
The examples in this book were written using Python 3.10 and pytest 7. pytest 7 supports Python 3.5 and above.
Why a Second Edition?
Both Python and pytest have changed since the first edition of this book was published in 2017. There have been updates to pytest that are now reflected in the book:
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.
So what’s in all of these new chapters?
I consolidated the information about debugging test failures into a chapter of its own. In the last edition, this information was spread all throughout the book. It is my hope that when you are faced with a deadline and a failing test suite, bringing this information together into one chapter will help you figure out an answer quickly and ease some stress.
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:
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 appropriate) 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.