Jump to ratings and reviews
Rate this book

Python Testing with pytest: Simple, Rapid, Effective, and Scalable

Rate this book
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


The examples in this book were written using Python 3.10 and pytest 7. pytest 7 supports Python 3.5 and above.

521 pages, Kindle Edition

Published February 21, 2022

50 people are currently reading
62 people want to read

About the author

Brian Okken

4 books2 followers

Ratings & Reviews

What do you think?
Rate this book

Friends & Following

Create a free account to discover what your friends think of this book!

Community Reviews

5 stars
17 (30%)
4 stars
30 (54%)
3 stars
7 (12%)
2 stars
1 (1%)
1 star
0 (0%)
Displaying 1 - 8 of 8 reviews
Profile Image for Andrii Borovyi.
43 reviews4 followers
August 10, 2022
Few years ago, after Brian Okken was an invited to Michael Kennedy's podcast "Talk Python with Me", I got the first edition of this book. And all these years I had no time to read it. This year I finally decided to read it, and when I started, I bumped into few things related with the new pytest's functionality. Fortunately, new edition appeared, so I thought it worth to save a bunch of time with few dollars. And... This book is really good.

It starts with few simple examples of how to install pytest (if you are at the entry level, it worth to read it), then installing the project you'll test almost all through the book. And here the problems came to me. I assume, that's because a companion code contains a complete set of code, mentioned in each chapter, and I was expected to write that code manually. So, each time I started chapter following only book instructions, and not paying attention to the code. So, few times I was in a situation, when my code and provided examples in the book were different. And that was disgusting. I believe, other people, who will follow provided code strictly, won't bump into my case, but if you like to write your code, and follow the book only, like me, then be ready to check the companion code, in some cases, when it is not clear for you what to do.

Also, the book contains a bunch of typos. Not a huge amount, but I got a feeling that some of them were just "not updated" parts, after editing. Another thing is, that if you try to organize your code in separate directories per chapter (as I did, and that's how examples are showed in the book), then you'll have to discover missing parts by yourself. And if each chapter contained a list of files that must migrate from the previous directory, that would help much.

So my verdict is 4 out of 5 stars. You should definitely read this book, but I suggest you to buy ebook, as it updates periodically. And after it will be cleaned of all issues, you can buy paperback.
Profile Image for Regis Hattori.
148 reviews12 followers
February 9, 2024
I am not very proficient in Python. When I started to code professionally in other languages like Java, Ruby, JS, Kotlin, I started to read a lot of books about them to gain confidence. Because I started to use Python after a lot of experience, I thought I could "just code" and search for material when needed. This was partially true, but I like to know more about the tools I use so I decided to read something about Python but not a book that teaches how to do an if/else statement or a loop.

Because I value tests a lot and because I thought Pytest is much less legible and harder to use when compared to other test libraries of other languages like RSpec (Ruby) and Jest (JS), I decided to read something about it just to have a better comparison.

I think this book presents the lib very well with good examples.
This book has a part that talks about the strategy of tests that I don't like. It is too shallow and it does not match with the rest of the book.
Another part I didn't like is that it tests a CLI application. I think the book could test a web application with a database connection because I think this is a much more common situation.

Talking about Pytest, now I think it is much better than I thought before reading the book, but I still think it is not very legible. The fact it uses only one assertion for everything is good from one point of view because you don't need to think about what assertion/matcher you need to use, but I think it is less legible. And I think legibility is one of the more important things when talking about tests.

Also, Pytest seems to have a very rich ecosystem. I see some functionalities that are not present in other libs I know and there are a lot of plugins we can use for different situations.
Profile Image for Ravi Sinha.
320 reviews11 followers
September 7, 2022
Lovely book. I took my time with it and went really hands on, which was worth it. Pytest is undoubtedly the best testing framework for Python out there right now, and the books goes pretty deep into it, covering all kinds of aspects. While nobody can remember all the options and capabilities, the docs and the plugins will come in pretty handy as I strive to make this library a more regular part of my Python projects.
Profile Image for dsreads.
149 reviews6 followers
January 20, 2024
Some things could be explained better. Just the first example that I found flipping back through it: he explained the -v flag, then removed the -v flag and added the —vb=no flag, without explaining the diff between verbose and tracebacks. Just one example of the book doing something new without really explaining why.

Still the best pytest book out there and I’d recommend for sure, it is the GOAT testing library in any language
600 reviews11 followers
December 16, 2022
A good introduction to pytest and testing in the Python world in general. You learn all the important parts and will be able to test your own application with this newly gained knowledge. The second edition offers a few interesting updates and some clarifications.
2 reviews1 follower
August 26, 2022
Great intro, wish it went more into advanced mocking but it did pretty well.
143 reviews1 follower
February 5, 2024
Everything you ever wanted to know about Pytest and probably more. Much of this book will come in handy, some of it will probably never use, but it is good to know it exists.
Displaying 1 - 8 of 8 reviews

Can't find what you're looking for?

Get help and learn more about the design.