Published: September 2017
Out of Print
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.
Q: I’m new to Python (or I’ve just built my first app) and I know I should include testing. Will this book help get me started?
A: 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.
Q: What is a test framework? The book refers to pytest as a testing framework. What does that mean?
A: 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.
Q: What makes pytest stand out above other test frameworks?
A: Here are a few of the reasons pytest stands out:
Q: My application is very different than the example application in the book. Will I still benefit from it?
A: Yes. I chose an example application that has a lot in common with many other types of applications. It has:
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.
Q: Does the code work with 2.7 and 3.x?
A: 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.
Q: Can I test web applications with pytest?
A: 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.
What Is pytest?
Learn pytest While Testing an Example Application
How This Book Is Organized
What You Need to Know
Example Code and Online Resources
Published: September 2017
Out of Print