- Paperback: 216 pages
- Publisher: Pragmatic Bookshelf; 1 edition (September 25, 2017)
- Language: English
- ISBN-10: 9781680502404
- ISBN-13: 978-1680502404
- ASIN: 1680502409
- Product Dimensions: 7.5 x 0.5 x 9.2 inches
- Shipping Weight: 14.4 ounces (View shipping rates and policies)
- Average Customer Review: 17 customer reviews
- Amazon Best Sellers Rank: #114,384 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.
Fulfillment by Amazon (FBA) is a service we offer sellers that lets them store their products in Amazon's fulfillment centers, and we directly pack, ship, and provide customer service for these products. Something we hope you'll especially enjoy: FBA items qualify for FREE Shipping and Amazon Prime.
If you're a seller, Fulfillment by Amazon can help you grow your business. Learn more about the program.
Frequently bought together
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.
Review this product
17 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.
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
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.
I don't regret reading it, but I can't unconditionally recommend it to others.