About This Title

Pages: 250
Published: January 2022
ISBN: 9781680508604
Edition: 1
In Beta

Skill Level Meter

Python Testing with pytest, Second Edition

Simple, Rapid, Effective, and Scalable

by Brian Okken

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.

eBook Formats:

  • PDF for desktop/tablets

  • epub for Apple Books, e-readers

  • mobi for Kindle readers

Get all eBook formats here for $24.95 (USD)

Add to Cart we accept visa, mastercard, amex, discover, paypal

This book is in Beta, final version expected Jan 2022

Beta Books: What do I get?


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.9 and pytest 6. pytest 6 supports Python 3.5 and above.

Resources

Releases:

  • B7.0 2021/10/21
  • B6.0 2021/10/07
  • B5.0 2021/09/16
  • B4.0 2021/08/25

Contents & Extracts

Note: Contents and extracts of beta books will change as the book is developed.

Preface

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

Author

Brian Okken is the host of the Test & Code podcast, and co-host of the Python Bytes podcast. He’s also a lead software engineer.

eBook Formats:

  • PDF for desktop/tablets

  • epub for Apple Books, e-readers

  • mobi for Kindle readers

Get all eBook formats here for $24.95 (USD)

Add to Cart we accept visa, mastercard, amex, discover, paypal

This book is in Beta, final version expected Jan 2022

Beta Books: What do I get?

Related Titles:

Skill Level Meter

About This Title

Pages: 250
Published: January 2022
ISBN: 9781680508604
Edition: 1
In Beta