small medium large xlarge

Python Testing with pytest: Simple, Rapid, Effective, and Scalable


Cover image for Python Testing with pytest

Python Testing with pytest

Simple, Rapid, Effective, and Scalable


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.

Choose Your Format(s)

  • $23.95 In Stock
    • Beta: What do I get?

  • Ebooks are DRM free.

  • Ebook delivery options.

  • The Paper Book will ship on 2017-10-10 (roughly).

About this Title

Pages: 190 (est)
Published: 2017-10-10
Release: B5.0 (2017-07-29)
ISBN: 978-1-68050-240-4

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.

Contents & Extracts

This book is currently in beta, so the contents and extracts will change as the book is developed.

  • 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
  • Getting Started with pytest
    • Getting pytest
    • Running pytest
    • Running Only One Test
    • Using Options
    • Exercises
    • What’s Next
  • Writing Test Functions
    • Testing a Package
    • Using Assert Statements
    • Expecting Exceptions
    • Marking Test Functions
    • Skipping Tests
    • Marking Tests As Expecting to Fail
    • Running a Subset of Tests
    • Parameterized Testing
    • Exercises
    • What’s Next
  • pytest Fixtures excerpt
    • Sharing Fixtures Through
    • Using Fixtures for Setup and Teardown
    • Tracing Fixture Execution with –setup-show
    • Using Fixtures for Test Data
    • Using Multiple Fixtures
    • Specifying Fixture Scope
    • Specifying Fixtures with usefixtures
    • Using autouse for Fixtures that Always Get Used
    • Renaming Fixtures
    • Parameterizing Fixtures
    • Exercises
    • What’s Next
  • Builtin Fixtures
    • Using tmpdir and tmpdir_factory
    • Using pytestconfig
    • Using cache
    • Using capsys
    • Using monkeypatch
    • Using doctest_namespace
    • Using recwarn
    • Exercises
    • What’s Next
  • Plugins
    • Finding Plugins
    • Installing Plugins
    • Writing Your Own Plugins
    • Creating an Installable Plugin
    • Testing Plugins excerpt
    • Creating a Distribution
    • Exercises
    • What’s Next
  • Configuration
  • Using pytest with Other Tools
  • Virtual Environments
  • pip
  • Plugin Sampler Pack
  • Packaging and Distributing Python Projects
  • xUnit Fixtures


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.