Python Testing with pytest

Book description

Test applications, packages, and libraries large and small with pytest, Python's most powerful testing framework. pytest helps you write tests quickly and keep them readable and maintainable. In this fully revised edition, explore pytest's superpowers - simple asserts, fixtures, parametrization, markers, and plugins - while creating simple tests and test suites against a small database application. 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. This book shows you how.

pytest is undeniably the best choice for testing Python projects. It's a full-featured, flexible, and extensible testing framework. pytest's fixture model allows you to share test data and setup procedures across multiple layers of tests. The pytest framework gives you powerful features such as assert rewriting, parametrization, markers, plugins, parallel test execution, and clear test failure reporting - 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 yet powerful tool. Write short, maintainable tests that elegantly express what you're testing. Speed up test times by distributing tests across multiple processors and running tests in parallel. Use Python's builtin assert statements instead of awkward assert helper functions to make your tests more readable. Move setup code out of tests and into fixtures to separate setup failures from 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, and even existing unittest tests.

Write simple, maintainable tests quickly with pytest.

What You Need:

The examples in this book were written using Python 3.10 and pytest 7. pytest 7 supports Python 3.5 and above.

Publisher resources

View/Submit Errata

Table of contents

  1.  Acknowledgments
  2.  Preface
    1. Why pytest?
    2. Learn pytest While Testing a Sample Application
    3. How This Book Is Organized
    4. What You Need to Know
    5. Why a Second Edition?
    6. Example Code and Online Resources
  3. Part I. Primary Power
    1. 1. Getting Started with pytest
      1. Installing pytest
      2. Running pytest
      3. Review
      4. Exercises
      5. What’s Next
    2. 2. Writing Test Functions
      1. Installing the Sample Application
      2. Writing Knowledge-Building Tests
      3. Using assert Statements
      4. Failing with pytest.fail() and Exceptions
      5. Writing Assertion Helper Functions
      6. Testing for Expected Exceptions
      7. Structuring Test Functions
      8. Grouping Tests with Classes
      9. Running a Subset of Tests
      10. Review
      11. Exercises
      12. What’s Next
    3. 3. pytest Fixtures
      1. Getting Started with Fixtures
      2. Using Fixtures for Setup and Teardown
      3. Tracing Fixture Execution with –setup-show
      4. Specifying Fixture Scope
      5. Sharing Fixtures through conftest.py
      6. Finding Where Fixtures Are Defined
      7. Using Multiple Fixture Levels
      8. Using Multiple Fixtures per Test or Fixture
      9. Deciding Fixture Scope Dynamically
      10. Using autouse for Fixtures That Always Get Used
      11. Renaming Fixtures
      12. Review
      13. Exercises
      14. What’s Next
    4. 4. Builtin Fixtures
      1. Using tmp_path and tmp_path_factory
      2. Using capsys
      3. Using monkeypatch
      4. Remaining Builtin Fixtures
      5. Review
      6. Exercises
      7. What’s Next
    5. 5. Parametrization
      1. Testing Without Parametrize
      2. Parametrizing Functions
      3. Parametrizing Fixtures
      4. Parametrizing with pytest_generate_tests
      5. Using Keywords to Select Test Cases
      6. Review
      7. Exercises
      8. What’s Next
    6. 6. Markers
      1. Using Builtin Markers
      2. Skipping Tests with pytest.mark.skip
      3. Skipping Tests Conditionally with pytest.mark.skipif
      4. Expecting Tests to Fail with pytest.mark.xfail
      5. Selecting Tests with Custom Markers
      6. Marking Files, Classes, and Parameters
      7. Using “and,” “or,” “not,” and Parentheses with Markers
      8. Being Strict with Markers
      9. Combining Markers with Fixtures
      10. Listing Markers
      11. Review
      12. Exercises
      13. What’s Next
  4. Part II. Working with Projects
    1. 7. Strategy
      1. Determining Test Scope
      2. Considering Software Architecture
      3. Evaluating the Features to Test
      4. Creating Test Cases
      5. Writing a Test Strategy
      6. Review
      7. Exercises
      8. What’s Next
    2. 8. Configuration Files
      1. Understanding pytest Configuration Files
      2. Saving Settings and Flags in pytest.ini
      3. Using tox.ini, pyproject.toml, or setup.cfg in place of pytest.ini
      4. Determining a Root Directory and Config File
      5. Sharing Local Fixtures and Hook Functions with conftest.py
      6. Avoiding Test File Name Collision
      7. Review
      8. Exercises
      9. What’s Next
    3. 9. Coverage
      1. Using coverage.py with pytest-cov
      2. Generating HTML Reports
      3. Excluding Code from Coverage
      4. Running Coverage on Tests
      5. Running Coverage on a Directory
      6. Running Coverage on a Single File
      7. Review
      8. Exercises
      9. What’s Next
    4. 10. Mocking
      1. Isolating the Command-Line Interface
      2. Testing with Typer
      3. Mocking an Attribute
      4. Mocking a Class and Methods
      5. Keeping Mock and Implementation in Sync with Autospec
      6. Making Sure Functions Are Called Correctly
      7. Creating Error Conditions
      8. Testing at Multiple Layers to Avoid Mocking
      9. Using Plugins to Assist Mocking
      10. Review
      11. Exercises
      12. What’s Next
    5. 11. tox and Continuous Integration
      1. What Is Continuous Integration?
      2. Introducing tox
      3. Setting Up tox
      4. Running tox
      5. Testing Multiple Python Versions
      6. Running tox Environments in Parallel
      7. Adding a Coverage Report to tox
      8. Specifying a Minimum Coverage Level
      9. Passing pytest Parameters Through tox
      10. Running tox with GitHub Actions
      11. Review
      12. Exercises
      13. What’s Next
    6. 12. Testing Scripts and Applications
      1. Testing a Simple Python Script
      2. Testing an Importable Python Script
      3. Separating Code into src and tests Directories
      4. Defining the Python Search Path
      5. Testing requirements.txt-Based Applications
      6. Review
      7. Exercises
      8. What’s Next
    7. 13. Debugging Test Failures
      1. Adding a New Feature to the Cards Project
      2. Installing Cards in Editable Mode
      3. Debugging with pytest Flags
      4. Re-Running Failed Tests
      5. Debugging with pdb
      6. Combining pdb and tox
      7. Review
      8. Exercises
      9. What’s Next
  5. Part III. Booster Rockets
    1. 14. Third-Party Plugins
      1. Finding Plugins
      2. Installing Plugins
      3. Exploring the Diversity of pytest Plugins
      4. Running Tests in Parallel
      5. Randomizing Test Order
      6. Review
      7. Exercises
      8. What’s Next
    2. 15. Building Plugins
      1. Starting with a Cool Idea
      2. Building a Local conftest Plugin
      3. Creating an Installable Plugin
      4. Testing Plugins with pytester
      5. Testing Multiple Python and pytest Versions with tox
      6. Publishing Plugins
      7. Review
      8. Exercises
      9. What’s Next
    3. 16. Advanced Parametrization
      1. Using Complex Values
      2. Creating Custom Identifiers
      3. Parametrizing with Dynamic Values
      4. Using Multiple Parameters
      5. Using Indirect Parametrization
      6. Review
      7. Exercises
      8. What’s Next
  6. A1. Virtual Environments
  7. A2. pip

Product information

  • Title: Python Testing with pytest
  • Author(s): Brian Okken
  • Release date: February 2022
  • Publisher(s): Pragmatic Bookshelf
  • ISBN: 9781680508604