![Kindle app logo image](https://m.media-amazon.com/images/G/01/kindle/app/kindle-app-logo._CB668847749_.png)
Download the free Kindle app and start reading Kindle books instantly on your smartphone, tablet, or computer - no Kindle device required.
Read instantly on your browser with Kindle for Web.
Using your mobile phone camera - scan the code below and download the Kindle app.
Python Testing with pytest: Simple, Rapid, Effective, and Scalable 1st Edition
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 Need:
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.
- ISBN-109781680502404
- ISBN-13978-1680502404
- Edition1st
- PublisherPragmatic Bookshelf
- Publication dateOctober 24, 2017
- LanguageEnglish
- Dimensions7.25 x 0.5 x 9 inches
- Print length216 pages
There is a newer edition of this item:
$39.49
(87)
Only 19 left in stock (more on the way).
Customers who viewed this item also viewed
From the brand
![The Pragmatic Programmers](https://m.media-amazon.com/images/S/aplus-media-library-service-media/1856817a-9e83-4f04-97a2-035e310ba39d.__CR0,0,2928,1250_PT0_SX1464_V1___.png)
-
Testing & Version Control
-
The Pragmatic Programmers publishes hands-on, practical books on classic and cutting-edge software development and engineering management topics. We help professionals solve real-world problems, hone their skills, and advance their careers.
From the Publisher
Python Testing with pytest
![Python Testing with pytest: Simple, Rapid, Effective, and Scalable Brian Okken](https://m.media-amazon.com/images/S/aplus-media/vc/2baa346d-e299-47d5-8eb1-af547e31fcfd._SL300__.jpg)
What makes pytest stand out above other test frameworks?
Here are a few of the reasons pytest stands out:
- Simple tests are simple to write in pytest.
- Complex tests are still simple to write.
- Tests are easy to read.
- You can get started in seconds.
- You use assert to fail a test, not things like self.assertEqual() or self.assertLessThan(). Just assert.
- You can use pytest to run tests written for unittest or nose.
- You can use pytest to run tests written for unittest or nose.
- It’s so extensible and flexible that it will easily fit into your workflow.
Q&A with Author Brian Okken
I’m new to Python, and I know I should include testing. Will this book help get me started?
Yes. Although the goal of the book is to teach you how to effectively and efficiently use pytest, it does it in the context of a working application.
Throughout the book I discuss various testing topics that relate to my philosophy of testing. Although it isn't a book intended to teach you all you need to know about test strategy, there is some of that in there.
I don't assume much Python and/or testing experience.
Experienced folks won't get bored, either. I've had people tell me that they've been testing for years with pytest and realized while reading the book many ways to improve their testing.
What is a test framework? The book refers to pytest as a testing framework. What does that mean?
pytest is a software test 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. It can be used to test Python distributions. And it integrates easily with other tools like continuous integration and web automation.
Because it’s installed separately from your Python version, you can use the same latest version of pytest on legacy Python 2 (2.6 and above) and Python 3 (3.3 and above).
Will it take me a long time to read enough of the book to get started testing?
I think you could start writing tests for your own projects right away, say after or during the first chapter. However, I highly recommend reading Chapter 2 before you write much of your test suite, because Chapter 2 will teach you ways to write tests more efficiently.
Chapter 1 will get you started right away with installing pytest and running some tests. It also covers a bunch of useful options for running pytest. Chapter 2 covers powerful pytest features useful for writing test functions.
And, it just builds from there. Fixtures, covered in Chapter 3, will help you organize your test code, separating common setup into sharable fixtures. The built-in fixtures in Chapter 4 will save you time with some common tedious code.
I think you can be writing test code for your own projects while reading these first four chapters, and the final three chapters will supercharge the testing of your projects and help you share test code with other projects.
Do I need to read it from cover to cover? Or can I pick and choose?
You don't need to read it cover to cover. The later chapters assume you are familiar with earlier topics, and the source code builds on previous chapters. However, I've also written the book to be a good reference to re-read sections when you need them.
The source code is downloadable (see url in the book) and is split into chapters, so you can jump in with the code for any individual chapter. We also back-reference material from other chapters to help you jump around.
However, it is a pretty quick read, and you may want to skim the whole thing to see what's there before focusing on the topic you really need right now.
![Python Testing with pytest: Simple, Rapid, Effective, and Scalable](https://m.media-amazon.com/images/S/aplus-media/vc/bc99155f-853b-4b48-b0dc-f427cb4726ea._SL300__.jpg)
My app is very different than the example app in the book. Will I still benefit from it?
Yes. I chose an example application that has a lot in common with many other types of applications. It has:
- A main user interface that is inconvenient to test against.
- Several layers of abstraction.
- Intermediate data types that are used for communication between components.
- A database data store.
Specifically, it's a command-line application called `tasks` that is usable as a shared to-do list for a small team. Although the specifics of this application might not be that similar to your own project, the overall structure in the bullet points above share testing problems with many other projects.
Can I test web applications with pytest?
Yes. pytest is being used to test any type of web application from the outside with the help of selenium, requests, and other web-interaction libraries. For internal testing, pytest been used by with Django, Flask, Pyramid, and other frameworks.
Does the code work with 2.7 and 3.x?
Yes. However, some of the example code uses the Python 3 style print function, `print('something')`. To run this code in Python 2.7, you'll need to add `from __future__ import print_function` to the top of those files.
Editorial Reviews
Review
Systematic software testing, especially in the Python community, is often either completely overlooked or done in an ad hoc way. Many Python programmers are completely unaware of the existence of pytest. Brian Okken takes the trouble to show that software testing with pytest is easy, natural, and even exciting.- Dmitry Zinoviev, Author of Data Science Essentials in Python
This book is the missing chapter absent from every comprehensive Python book.- Frank Ruiz, Principal Site Reliability Engineer, Box, Inc.
About the Author
Product details
- ASIN : 1680502409
- Publisher : Pragmatic Bookshelf; 1st edition (October 24, 2017)
- Language : English
- Paperback : 216 pages
- ISBN-10 : 9781680502404
- ISBN-13 : 978-1680502404
- Item Weight : 14.4 ounces
- Dimensions : 7.25 x 0.5 x 9 inches
- Best Sellers Rank: #1,119,152 in Books (See Top 100 in Books)
- #412 in Software Testing
- #1,252 in Python Programming
- #1,344 in Software Development (Books)
- Customer Reviews:
About the author
![Brian Okken](https://m.media-amazon.com/images/I/71HIK+33PoL._SY600_.jpg)
Discover more of the author’s books, see similar authors, read book recommendations and more.
Customer reviews
Customer Reviews, including Product Star Ratings help customers to learn more about the product and decide whether it is the right product for them.
To calculate the overall star rating and percentage breakdown by star, we don’t use a simple average. Instead, our system considers things like how recent a review is and if the reviewer bought the item on Amazon. It also analyzed reviews to verify trustworthiness.
Learn more how customers reviews work on AmazonCustomers say
Customers find the book informative and useful as a reference guide. They describe it as an easy, well-written read with good pacing. The book provides a quick path to start implementing tests in Python projects.
AI-generated from the text of customer reviews
Customers find the book informative and useful as a reference. They describe it as a quick read with great examples that make their work more usable. The book stays on topic and covers making pytest work for Python units.
"...This book should scratch that itch. It stays on topic and talks about making pytest work for python unit-testing - no more, no less; it completely..." Read more
"...It was a nice and quick read and good for a quick reference guide." Read more
"Quick and to the point, this book is very helpful to get working quickly with pytest...." Read more
"...It works well to use as a reference and dip into on occasion. 5 stars if he would include more in two areas:..." Read more
Customers find the book easy to read and well-written. They describe it as a short, well-edited book.
"...This is a short well-edited book and an easy read...." Read more
"...It was a nice and quick read and good for a quick reference guide." Read more
"Very well-written book, and wonderful accompanying github repo...." Read more
"Book is easy to read and follow" Read more
Customers find the book helpful for getting started with pytest quickly. They mention it's a good book about testing Python code. The book stays on topic and talks about making pytest work for unit-testing.
"...It stays on topic and talks about making pytest work for python unit-testing - no more, no less; it completely delivers on the promise of the..." Read more
"Quick and to the point, this book is very helpful to get working quickly with pytest...." Read more
"...Pytest has many features and is a big toolkit (containing fixtures, test runner, annotations, and more)...." Read more
"Good book about testing your Python code. There are few books, if any other, that cover the same topic...." Read more
Top reviews from the United States
There was a problem filtering reviews right now. Please try again later.
- Reviewed in the United States on November 30, 2017If you just want to get started with unit-testing, skip this book.
If you're a python programmer who knows (a little or a lot of) unit-testing you may want to learn how to better exploit the built-in fixtures and other goodies pytest has to offer. This book should scratch that itch. It stays on topic and talks about making pytest work for python unit-testing - no more, no less; it completely delivers on the promise of the title.
This is a short well-edited book and an easy read. The first 150 pages include a fairly detailed overview and good (short) examples of the various cmd-line switches/options, a good discussion of conftest.py, and the other built-in fixtures. The last 30 pages or so is a series of short appendices about virtual environments, pip, pytest packages, and packaging python code. These are nice adjuncts that can help make your work more useable... and make you look good too. I was glad Brian avoided the subject of mocking, as that warrants either a book section or an entire book and anything less would be unfulfilling and distracting.
Really nice work.
- Reviewed in the United States on October 22, 2019I am scared of writing Tests but this book makes it look really easy. It was a nice and quick read and good for a quick reference guide.
- Reviewed in the United States on September 22, 2019Quick and to the point, this book is very helpful to get working quickly with pytest. If you are looking for a book about why to test and software engineering theory this is probably not the best choice. If you are are familiar with the ideas behind test driven development and want to get pointers on the pythonic way to get that done this is probably among the most efficient ways.
- Reviewed in the United States on April 29, 2019Very well-written book, and wonderful accompanying github repo.
The only downside is that this book is really just about pytest, and not about testing in general. What I mean is, you will need to supplement this book with more information, either Google searches or another textbook. For instance, the author spends about 1 or 2 pages on tox. Packaging is covered in only a cursory fashion in the appendix. And he does not mention behavioral testing (IIRC) or end-to-end testing.
These are very minor issues, in my opinion, since the author makes no claims to cover these other things.
- Reviewed in the United States on November 23, 2020I've been a developer for almost 30 years. I've used many test suites over that time. And these days, between on line documents and YouTube videos, recommending a book seems unspeakably "20th century." But I do recommend this book. I'm a latecomer to Python (I'm part of the perl generation). So this book, which is firmly grounded in established software testing practices and theories, takes a "here's what's unique to this language and this testing framework." It does this so well that it has improved my understanding of Python styles and idioms let alone getting me in to the depths of pytest, and what it offers that really makes it more effective and compact than any testing framework I've used before. And having the text there while you work with code is genuinely helpful. Plus books are solar powered and the batteries never run out!
- Reviewed in the United States on February 23, 2018I bought this book because I wanted an organized walkthrough of Pytest. It delivered. Pytest has many features and is a big toolkit (containing fixtures, test runner, annotations, and more). Admittedly you can browse through the online docs or search through various presentations to get most of the data. But if you value your time and want to make sure that you don't miss out on an important feature, I highly recommend this book. It does a great job of explaining the options for invoking pytest (hint use -v), best practices for file layout, how to use fixtures, and extending pytest with plugins. If you work with Python you owe it to yourself to get this book.
- Reviewed in the United States on July 13, 2018Mr. Okken, who I learned about through his Python Bytes webcast, did a nice job on this first edition of the book. It works well to use as a reference and dip into on occasion.
5 stars if he would include more in two areas:
1) best practices on organizing testing files and getting tests to run in a predetermined order (so that "basic" tests will be caught early in the test cycle)
2) how to best integrate with buyilt in logging module. I discovered this by accident when I added logging that Pytest captures it for output. It would be nice to see his professional opinions on it
- Reviewed in the United States on January 26, 2023This book on pytest is what you need for python testing.
Top reviews from other countries
- WaldirioReviewed in Canada on May 2, 2022
5.0 out of 5 stars very good book
if you are looking for something to start with pytest, this is the book. Very detailed and with a lot of tricks. I totally recommend it.
-
schelli666Reviewed in Germany on June 6, 2021
4.0 out of 5 stars Guter Einstieg
Guter Einstiegin das Thema Pytest. Leicht verständlich.
Die Einrichtung der Beispiele hat nicht fehlerfrei funktionert. Dafür der Punktabzug.
Ansonsten gefälltmir das Buch sehr gut.
- AndrewReviewed in the United Kingdom on February 15, 2021
5.0 out of 5 stars Good introduction to using pytest
I like the way the book is laid out. It doesn't assume you're an all round expert - and walks you through installing and setting various things up (via appendices) as well as providing decent clear descriptions of other Python related topics such as packaging. Exactly the sort of information, collated in one place, that is really useful.
If you're a reasonably experienced developer and want to add some testing to your Python work - this would be an excellent place to start.
-
Vicente MarçalReviewed in Brazil on June 11, 2018
5.0 out of 5 stars Indispensável
Excelente livro para quem está iniciando ou já trabalha com o framework de testes pytest. Didático e com foco no uso prático apresenta os conceitos de forma clara e é um excelente guia. Não deve faltar na biblioteca de um Desenvolvedro Python.
- A RReviewed in the United Kingdom on September 29, 2018
5.0 out of 5 stars Drastically Improved My Understanding of Pytest
In my opinion anyone trying to write Python unit-tests will really appreciate this book if:
1) one has obviously been using Python
2) and preferably has basic understanding and use of Python's built-in Unittest framework.
Although I had dabbled with the Pytest unittesting framework before, this time round it made a whole lot more sense.
The author did a fantastic job of providing easy to follow and applicable examples of some of Pytest's major features (the ones a developer is most likely to use on a day-to-day basis). The official Pytest docs can be arduous to understand, but this book has become an invaluable tool.