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.

Customer Reviews

I found Python Testing with pytest to be an eminently usable introductory guidebook to the pytest testing framework. It is already paying dividends for me at my company.

- Chris Shaver

VP of Product, Uprising Technology

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.

See All Reviews

Choose Your Format(s)

  • $45.95 In Stock

    Save $13.95 on the combo pack.

  • $35.95 In Stock
  • $23.95 In Stock
  • Ebooks are DRM free.

  • Ebook delivery options.

What You Need

The examples in this book were written using Python 3.6 and pytest 3.2. pytest 3.2 supports Python 2.6, 2.7, and Python 3.3+.

Contents & Extracts

  • Acknowledgments
  • 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
    • Parametrized 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
    • Parametrizing 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
    • Understanding pytest Configuration Files
    • Changing the Default Command-Line Options
    • Registering Markers to Avoid Marker Typos
    • Requiring a Minimum pytest Version
    • Stopping pytest from Looking in the Wrong Places
    • Specifying Test Directory Locations
    • Changing Test Discovery Rules
    • Disallowing XPASS
    • Avoiding Filename Collisions
    • Exercises
    • What’s Next
  • Using pytest with Other Tools
    • pdb: Debugging Test Failures
    • Determining How Much Code Is Tested
    • mock: Swapping Out Part of the System
    • tox: Testing Multiple Configurations
    • Jenkins CI: Automating Your Automated Tests
    • unittest: Running Legacy Tests with pytest
    • Exercises
    • What’s Next
  • Virtual Environments
  • pip
  • Plugin Sampler Pack
    • Plugins That Change the Normal Test Run Flow
    • Plugins That Alter or Enhance Output
    • Plugins for Static Analysis
    • Plugins for Web Development
  • Packaging and Distributing Python Projects
    • Creating an Installable Module
    • Creating an Installable Package
    • Creating a Source Distribution and Wheel
    • Creating a PyPI-Installable Package
  • xUnit Fixtures
    • Syntax of xUnit Fixtures
    • Mixing pytest Fixtures and xUnit Fixtures
    • Limitations of 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.