- Paperback: 222 pages
- Publisher: Pragmatic Bookshelf; 1 edition (September 25, 2017)
- Language: English
- ISBN-10: 1680502409
- ISBN-13: 978-1680502404
- Product Dimensions: 7.5 x 0.5 x 9.2 inches
- Shipping Weight: 14.4 ounces (View shipping rates and policies)
- Average Customer Review: 12 customer reviews
- Amazon Best Sellers Rank: #353,723 in Books (See Top 100 in Books)
Enter your mobile number or email address below and we'll send you a link to download the free Kindle App. Then you can start reading Kindle books on your smartphone, tablet, or computer - no Kindle device required.
To get the free app, enter your mobile phone number.
Python Testing with pytest: Simple, Rapid, Effective, and Scalable 1st Edition
Use the Amazon App to scan ISBNs and compare prices.
"Children of Blood and Bone"
Tomi Adeyemi conjures a stunning world of dark magic and danger in her West African-inspired fantasy debut. Learn more
Frequently bought together
Customers who bought this item also bought
Customers who viewed this item also viewed
From the Publisher
Python Testing with pytest
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.
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.
I found Python Testing with pytest to be an eminently usable introductory guidebookto the pytest testing framework. It is already paying dividends for me at mycompany.
About the Author
Brian Okken is a lead software engineer with two decades of R&D experience developing test and measurement instruments. He hosts the Test & Code podcast and co-hosts the Python Bytes podcast.
Author interviews, book reviews, editors picks, and more. Read it now
Top customer reviews
There was a problem filtering reviews right now. Please try again later.
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.
This book got it right. I wanted to "level up" on pytest, without devoting 800 pages, but still something that covered all the topics in enough depth that I could start using those features and even refer back to the book as a reference. With a heavy emphasis on working code, Brian left me in a spot where I was effective at test-writing.
Kudos on the effort to produce variations of working code. I'm in a similar business and understand how hard it is to get that right. I followed the numerous pre-release versions (kudos also on that) and I can only imagine the ongoing maintenance.
Perhaps my only quibbles: a number of times a topic was introduced and then "we'll cover that in Chapter 4". A few times is ok but it got a bit excessive. I would have liked a deeper treatment of mocking, as this is a topic with much subtlety.
Thanks Brian for the hard work.
Let's face it, testing is hard. It is a skill you have to practice and like any other skill you need to learn it first.
Enter Brian Okken's "Python testing with pytest".
It is one of those tech books that are a joy to read. The book is not only informative but well written (and it has one of the loveliest acknowledgements ever).
Despite being relatively short, Python testing with pytest is packed with information.
Okken takes us quickly through the setup, managing to cover the basics of running tests and available options. This alone is enough to give you a feel for pytest.
We are then shown the basics of writing test functions. Okken takes the time to demonstrate some of pytest's best features (marking test function, parameterization, etc.) and makes a point of testing for failure (something a lot of us forget, I think).
The book goes on to discuss fixtures, both built-in and those you write yourself, and configuration, as well as plugins and how to use pytest with other tools.
I honestly think this book made me a little better at testing. Yes, it is about pytest but it presents testing in such a way that my eyes don't glaze over as they sometimes might.
This book is useful for learning the tool and the skill. Highly recommended!