O'Reilly logo

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Python Testing with pytest

Book Description

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.

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.

Table of Contents

  1.  Acknowledgments
  2.  Preface
    1. What Is pytest?
    2. Learn pytest While Testing an Example Application
    3. How This Book Is Organized
    4. What You Need to Know
    5. Example Code and Online Resources
  3. 1. Getting Started with pytest
    1. Getting pytest
    2. Running pytest
    3. Running Only One Test
    4. Using Options
    5. Exercises
    6. What’s Next
  4. 2. Writing Test Functions
    1. Testing a Package
    2. Using assert Statements
    3. Expecting Exceptions
    4. Marking Test Functions
    5. Skipping Tests
    6. Marking Tests as Expecting to Fail
    7. Running a Subset of Tests
    8. Parametrized Testing
    9. Exercises
    10. What’s Next
  5. 3. pytest Fixtures
    1. Sharing Fixtures Through conftest.py
    2. Using Fixtures for Setup and Teardown
    3. Tracing Fixture Execution with –setup-show
    4. Using Fixtures for Test Data
    5. Using Multiple Fixtures
    6. Specifying Fixture Scope
    7. Specifying Fixtures with usefixtures
    8. Using autouse for Fixtures That Always Get Used
    9. Renaming Fixtures
    10. Parametrizing Fixtures
    11. Exercises
    12. What’s Next
  6. 4. Builtin Fixtures
    1. Using tmpdir and tmpdir_factory
    2. Using pytestconfig
    3. Using cache
    4. Using capsys
    5. Using monkeypatch
    6. Using doctest_namespace
    7. Using recwarn
    8. Exercises
    9. What’s Next
  7. 5. Plugins
    1. Finding Plugins
    2. Installing Plugins
    3. Writing Your Own Plugins
    4. Creating an Installable Plugin
    5. Testing Plugins
    6. Creating a Distribution
    7. Exercises
    8. What’s Next
  8. 6. Configuration
    1. Understanding pytest Configuration Files
    2. Changing the Default Command-Line Options
    3. Registering Markers to Avoid Marker Typos
    4. Requiring a Minimum pytest Version
    5. Stopping pytest from Looking in the Wrong Places
    6. Specifying Test Directory Locations
    7. Changing Test Discovery Rules
    8. Disallowing XPASS
    9. Avoiding Filename Collisions
    10. Exercises
    11. What’s Next
  9. 7. Using pytest with Other Tools
    1. pdb: Debugging Test Failures
    2. Coverage.py: Determining How Much Code Is Tested
    3. mock: Swapping Out Part of the System
    4. tox: Testing Multiple Configurations
    5. Jenkins CI: Automating Your Automated Tests
    6. unittest: Running Legacy Tests with pytest
    7. Exercises
    8. What’s Next
  10. A1. Virtual Environments
  11. A2. pip
  12. A3. Plugin Sampler Pack
    1. Plugins That Change the Normal Test Run Flow
    2. Plugins That Alter or Enhance Output
    3. Plugins for Static Analysis
    4. Plugins for Web Development
  13. A4. Packaging and Distributing Python Projects
    1. Creating an Installable Module
    2. Creating an Installable Package
    3. Creating a Source Distribution and Wheel
    4. Creating a PyPI-Installable Package
  14. A5. xUnit Fixtures
    1. Syntax of xUnit Fixtures
    2. Mixing pytest Fixtures and xUnit Fixtures
    3. Limitations of xUnit Fixtures