Do less work when testing your Python code, but be just as expressive, just as elegant, and just as readable. The pytest testing framework helps you write tests quickly and keep them readable and maintainable - with no boilerplate code. 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 for applications, packages, and libraries. This book shows you how. For Python-based projects, pytest is the undeniable choice to test your code if you're looking for a full-featured, API-independent, flexible, and extensible testing framework. With a full-bodied fixture model that is unmatched in any other tool, the pytest framework gives you powerful features such as assert rewriting and plug-in capability - 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 and robust tool. Write short, maintainable tests that elegantly express what you're testing. Add powerful testing features and still speed up test times by distributing tests across multiple processors and running tests in parallel. Use the built-in assert statements to reduce false test failures by separating setup and 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, unittest, and doctest. Write simple, maintainable tests that elegantly express what you're testing and why. What You The examples in this book are written using Python 3.6 and pytest 3.0. However, pytest 3.0 supports Python 2.6, 2.7, and Python 3.3-3.6.
It covers a lot of ground for such a brief read. Would recommend to anyone starting out with Python, just to get the grip of how things should be built. For me personally, it filled in a lot of gaps in testing code with Python, which is the whole reason I bought this book.
Though I'm already well versed in pytest usage, I enjoyed reading this book and found some interesting little gems which will definitely be useful for some of my projects.
I admit, this book is about two of my favorite topics, namely unit-testing and Python.
The online documenation about pytest is quite good, so the question is, why a book on pytest?
Also, a simple google search can land you on multiple pages with a lot of answers; again, why a book?
The problem with online documentation and results of google searches is that quite often you end up with an answer to a specific question, like: "How do I do X"; sometimes the answer is not exactly X, but it's usually enough for you to figure the rest by yourself.
What's the problem here? The problem is that when you have another question, you still have to make another search. What you are missing, in short, is a good model of the tool which you are using.
This book delivers on this, providing a good model of pytest tool. When you have such a model, even if you don't know the answer, you will understand how to find it.
This book is very, very good. In terms of being a thorough introduction to pytest, it is the best there is, and it is nearly perfect. In terms of introducing test-driven design, Brian Okken is not quite ambitious enough. If you haven't read a book about test-driven design, get one along with this book. One homes a future edition shows the TDD process through pytest, which is the best way to learn it.
The book is essentially a more entertaining version of the pytest documentation. It's short, has a lot of code snippets, and sometimes provides useful information beyond the documentation. For example, I picked up a couple of new libraries and found 3-4 pytest techniques, which I didn't know before.
I adjusted my rating from 5 to 4 stars since this book overlooks at least one crucial nuance in utilizing pytest.
When you write tests, you mock many things, including calls to requests to external APIs. Once you've imported a module into another, the namespace of the imported module changes. It can pose challenges when attempting to mock.patch something in the imported module. Your code might stubbornly continue to call the method you have patched, causing complications.
This issue arises with notable frequency. Effectively addressing it requires a comprehensive grasp of how Python's namespace changes after importing. Regrettably, the book overlooks this topic entirely. When faced with such a problem, you can find a solution within Stack Overflow answers. However, the search process can prove protracted and arduous.
=====
I have recently joined thousands of Python developers who have read this book, including its previous edition. IMHO the book is not so terrific, but it accomplishes its task of teaching you how to use pytest professionally.
The book has significant issues with pacing. Some chapters feels like they have almost no content and the text is just stretched out to fill the page count, mainly through long examples and trivial printouts of the pytest "-help" output. Other sections feels too short.
Overall, I would absolutely not recommend this book for learning TDD (for that, "Test Driven Web Development with Python" (the testing goat book) is an infinitely better option). It is also not very successful as a reference. It does work for getting an overview of the pytest features, before one moves over to the official documentation for the details.
Still giving it 3 stars, since it was a quick read (I finished it in just a couple of hours, including trying out examples), and I think I got a reasonably good overview of pytest from it. I do not regret reading it.
I read this book in order to get an introduction to Pytest, the testing framework associated with the Python programming language, and it proved to be a very thorough introduction indeed. I have a pretty solid background in Python but you don’t need to be an expert programmer to get a lot out of this book. The book does have a small, pre-written Python application called Cards which is used to teach about testing. The Cards app is just a small ticketing system but it helps if you know enough Python to read through and understand how it works. But don’t worry, this book does a great job of introducing a lot of other things besides Pytest.
The book walks you through how to write and run tests against your code. But it also goes way beyond that and talks about testing strategy, testing as it relates to Continuous Integration, and several advanced testing features and methods. It develops your test understanding by starting out with concepts from software testing culture like Arrange-Act-Assert and Given-When-Then and moving on to ideas like the “happy path”, code coverage, line coverage, branch coverage, mocks (also known as test doubles, spies, fakes, or stubs), fixtures, monkey-patching, mock drift, and “Testing Enough to Sleep at Night”. While many concepts are just touched on, it is very helpful to get exposed to all this testing jargon and concepts, especially if you are new to tests or don’t have much experience.
The book itself does not require much typing in of code as it comes with a very well-organized Source Code zip file with almost everything you need. Each chapter has a separate folder and as you work through the book you will find that all the code in the book is there in the Source Code, so all you need to do is type in the commands you’re given to follow along. Each chapter has a few exercises at the end and I found these to be hit or miss as to how well they helped me absorb the material. Some were really good at helping the reader solidify their understanding of the content of the chapter but a few were frustratingly challenging and some were almost not really exercises at all. For example, near the end of the book one exercise was pretty much to read the chapter: “Start at the beginning of the chapter and read and understand the code examples for each technique.” The exercises do not come with solutions either, which I wish was not the case.
But even if the exercises weren't where the author was devoting most of his attention, that’s okay because the rest of the book was really comprehensive, easy to follow along, and packed with a lot of material (but not too much). Besides testing there were also useful tidbits about things like virtual environments in Python, pip, the Python installer, and GitHub Actions. Overall, this is an excellent introduction to the Pytest framework, to testing software, and to testing Python applications. It’s readable, comprehensive, well-organized, and very educational.
It has been a long time since I do not understand programming without testing. When I did learn Python using the JetBrains Academy, I was missing my TDD approach, so I needed to learn the basics of testing in Python before going on with the exercises. I did it using this book. I read those chapters that I needed to get acquainted with the minimum set-up to have a working testing harness. Then I stopped reading.
Now I needed to work on an entirely new package, and I decided to read the book, create my notes for the future, and do proper testing on my python work.
After reading it, I can say that the book does the job, and that is it. No theory about testing and not deep dive into the internals of any tools. It goes direct to what tools `pytest` offers for the python tester. That is fantastic if you want to get going faster and bad if you need to understand the concepts behind or even get right the testing philosophy of the tool. Eventually, I will read the whole set of documentation on the `pytest.org` site.
I found several poor points: * The indexation of the code in the whole book is terrible to the point of being unreadable. * chapter 2 about testing a package is poorly explained on the interaction between the test and the package. It took me a while to realize that you are directly testing the package, not the code. * Appending 4 about packaging. Oh my! I did understand nothing on that one, and I had to read many other sources to know how a package works. * Appendix 1 about virtual environments. Come on! You can not just give the instruction without explaining the concept!
In general, I’m finding that the documentation in the python environment, books, articles, and even official documentation is poorly written compared to the Ruby world. All I read about ruby, and I read a lot, is more carefully written and more balanced. Of course, this is just my opinion, and it could be biased, but it is something that I face every time. Anyway, I’ll keep reading books about Python this year. I have two more already in the queue!
I thought this book was a pretty decent tour of the features in pytest, and the advantages it offers over the traditional unittest module. The choice to use a simple project to test with pytest was a good one; it really helped highlight the reasoning behind some of the features in pytest and how to use them effectively. Okken did a really good job of picking and choosing topics that were relevant without bogging the user down in too much detail. The focus on fixtures and plugins was an especially good choice, as designing an effective test suite in pytest really comes down to extending pytest in such a way that you remove as much of the boilerplate required to write tests as possible.
My only gripe about the book is that the code is only available as a downloadable zip file on Pragmatic Programmer's website, and that the code for different parts of each chapter lives in a separate directory. I would have preferred to have the code available in a public git repo (GitHub or otherwise), and then have the different parts of each chapter be available as a branch or a tag. This way you could diff between the different 'revisions' in each chapter and see how the code progresses as you use different features of pytest. Having the code in a public repo would also allow it to be updated for newer versions of pytest (changes to pytest since the publishing of the book cause some of the examples to not work out of the box).
If you write python in a personal or professional setting, I'd definitely recommend picking up this book as a guide on how to add tests to your code.
1. It is affiliated with The Pragmatic Programmer which I read before. 2. I wanted to strengthen my knowledge of TDD for CI/CD.
Regardless of this book being affiliated to TPP. It isn't as interesting as the original TPP. This book shouldn't exist as a book. Whatever you read in this book can be easily be found on pytest's documentation. This book should either exist in a YouTube playlist or a series of blog posts. I didn't find this book worth money (especially in India).
On the positive side, after reading this book, I found out what 'things' can be tested. pytest has a very large community.
Who should read this book? What's the correct way to get knowledge of everything taught in this book? The only reason you should buy this book is that you don't have to write your own code to test whatever is taught in this book. They provide their own code to test and exercise against. If you ask me how to gain knowledge of whatever is taught in this book. I'd say go take a large open-source python project (e.g. aws-cli) and look at their tests. If you don't understand their tests, refer to pytest's documentation.
Although this book primarily targets beginners, I picked up a few valuable things out of it. Also, this is one of those rare testing books that merely try to teach you a few concepts without urging you to sell your soul to a doctrine—in this case, TDD.
The book starts slowly. Instead of showing you a bunch of meaningless toy tests, it demonstrates how to test a simple CLI. The CLI code is simple enough so that you can focus on learning the Pytest API and the concepts around it—but complex enough to keep you hooked. Primarily the book is a sterling demonstration of how to do Subcuteneous Functional Tests properly while keeping things simple and loosely coupled.
My only gripe against the book is that the fonts are not the most legible thing in the world, and the digital version doesn't maintain proper spacing in the Python code. Along with that, the lack of syntax highlighting doesn't help. Overall, an excellent book for people who're either new to the testing world or just Pytest.
I've been looking for what turned out to be this book for a long time. Of course pytest is the standard testing tool for Python, but it is also full of powerful features, fixtures, etc. that can really help you hone in on testing. The book also does a very good job of covering the "theoretical" aspects of testing, best practices, etc. At the end of the day, not only am I more comfortable with the aspects that the book covered, but I feel more comfortable exploring the world of third party extensions.
If you have experience writing tests in Python and just want to refine skills, there are several topics, which might help. But majority of the content might look underwhelming.
If you haven't written any tests using Python yet - this might be valuable. However - this book won't give a tutorial how to write good tests. Only the tools and how to use them
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. A bit more about testing Flask applications would be appreciated. Nevertheless, I got a lot out of this book and the section about plugins and extensions will help you to improve your pytest experience even more.
Ótimo livro. Faz um overview completo sobre a ferramenta (Pytest), trazendo informações úteis sobre como elaborar, estruturar e organizar seus testes e fixtures. Aborda também diversas extensões para o Pytest (sem se tornar cansativo) e apresenta como escrever suas próprias extensões.
Diria que é um livro essencial para quem se interessa por testes de software em Python independente do nível de conhecimento.
This is an extremely biased rating. Niether the author or the contents of the book are at issue its more the assigning the book as a part of a curriculum to which students were not even capable of doing simple coding. Struggling through this book and being tested on this material was traumatic and until i work through that trauma this books rating will simply stand where its at.
It's really important to know how to test your code if it's complex, will be used by others, and/or will be upgraded and maintained over time. This book offers a solid intro to doing that testing. It assumes a fairly high level of proficiency with Python specifically and programming generally, which I didn't have going into the book, so I may not have been the target audience.
A valuable resource for one of the greatest tools in the Python ecosystem. The writing isn't always flawless, but the information is solid, whether you're new to pytest, or have been using it for a while, and are looking to get even more out of it.
Useful, practical book if you're looking to start a deep dive into pytest. Having limited experience writing tests in python, this was easy to follow and understand, and seems to have covered all the questions I would have had about getting going with a testing framework.
Pretty good introduction to TDD, doesn't have much to offer on BDD though! I think the examples could've been better and less redundant. However, overall a great book.