Development mode — payments are simulated and emails go to the Dev Mailbox · Payment simulator
DDSP
Cover Testing Python with pytest — page spread 1 Testing Python with pytest — page spread 2 Testing Python with pytest — page spread 3 Testing Python with pytest — page spread 4 Testing Python with pytest — page spread 5 Testing Python with pytest — page spread 6 Testing Python with pytest — page spread 7 Testing Python with pytest — sample page Testing Python with pytest — sample page Testing Python with pytest — sample page

Learn pytest from your first assert to a fast, reliable test suite running in CI: test design, assertions, discovery and selection, fixtures and conftest, factories and builders, parametrization, markers, tmp_path and monkeypatch, unittest.mock and fakes, time and randomness, HTTP testing with…

Fixtures, Parametrization, Mocking, Property-Based Testing, and Test Suites You Can Trust

Read before you buy: the PDF here is a free sample — the first 20 pages of the 513-page book (payments, sales and reviews on this demo site are sample data). Get the full e-book at thenexbooks on Gumroad.

Write tests that catch real bugs, run fast, and stay easy to change — with pytest, the way professional Python teams use it.

Most Python developers start testing with print statements and hope. It works until the code grows: a change to the discount rules breaks shipping, a refactor shifts a total by one cent, and nobody dares touch the checkout on a Friday. pytest makes automated testing pleasant — a test is a plain function with a plain assert — and behind that simple surface is a deep toolkit of fixtures, parametrization, markers, and plugins.

This book teaches pytest from the very first test to a suite you can trust in production. You test one realistic codebase step by step, and every technique appears at the moment it solves a real problem, so you learn how to design good tests for your own code, not just copy patterns.

What's inside

  • Why we test and what is worth testing, and how to design Python code that is easy to test: pure functions, seams, and injected dependencies
  • Writing and running tests: discovery, project layout, node IDs, -k and -m selection, --lf, --sw, and reading tracebacks and assertion diffs with confidence
  • Assertions in depth: pytest.raises with match, warnings, pytest.approx, Decimal for money, and helper functions that fail clearly
  • Fixtures from first principles: yield teardown, scopes, conftest.py, factory fixtures, test data builders, and parametrized fixtures
  • Parametrization with readable IDs, boundary tables, data files, and subtests; markers for skip, xfail, and custom categories
  • Isolation: tmp_path, monkeypatch, capsys, caplog, unittest.mock with autospec — and when a simple fake beats a mock
  • Hard-to-test code: time and dates, randomness, HTTP clients with httpx MockTransport, SQLite and SQLAlchemy databases, async code with pytest-asyncio, and command-line programs
  • Property-based testing with Hypothesis: strategies, shrinking, examples, settings, and stateful tests
  • Quality and workflow: coverage with pytest-cov, pytest configuration, writing your own hooks and plugins, curing flaky and slow tests, a complete TDD walkthrough, and type checking and linting test code with mypy and ruff
  • Continuous integration with GitHub Actions, maintaining a growing suite, test smells, legacy code, and integration and end-to-end tests

One running example, start to finish. You test the shop core of Tallowbrook Candle Co., a fictional small online candle shop: catalog and stock, carts, coupons, weight-based shipping with a free-shipping threshold, checkout and order numbers, a carrier rate API client, an order database, a command-line tool, CSV exports, gift codes, and loyalty points. A complete reference project — the package plus a suite of more than a hundred passing tests — goes with the book, and every test and output shown was produced by running the code with real Python and real pytest.

Current and honest. The book targets Python 3.12+ and pytest 9, notes what is new in pytest 9 and where older tutorials differ, and labels third-party plugins clearly. It contains no invented benchmarks or statistics. Every chapter includes runnable code with real output, Tips, Notes, Warnings, exercises, and Key Takeaways, and the appendices give you a pytest cheat sheet, copy-ready templates (pyproject.toml, conftest hooks, builders, contract tests, a GitHub Actions workflow), a production checklist for test suites, and a glossary.

Format: PDF, 513 pages (A4), linked table of contents and bookmarks. English. First Edition, 2026. Published by thenexbooks. This book is independent and is not affiliated with, sponsored by, or endorsed by the Python Software Foundation or the pytest project.

Table of Contents

Part I — pytest Foundations

  • Ch 1 · Why Test? What to Test, and What pytest Gives You
  • Ch 2 · Your First Tests: Installing pytest, Writing Tests, and Plain assert
  • Ch 3 · Running pytest: Discovery, Layout, Selection, and Reading Failures
  • Ch 4 · Assertions in Depth: Exceptions, Warnings, Approximate Values, and Clear Failures
  • Ch 5 · Designing Testable Code: Pure Functions, Seams, and the Tallowbrook Core

Part II — Fixtures and Parametrization

  • Ch 6 · Fixture Fundamentals: Setup, Teardown, and yield
  • Ch 7 · Fixture Scope, conftest.py, and Sharing Fixtures
  • Ch 8 · Factory Fixtures, Fixture Composition, and Test Data Builders
  • Ch 9 · Parametrization: One Test, Many Cases
  • Ch 10 · Markers: skip, xfail, Custom Markers, and Selecting Tests

Part III — Isolation and Test Doubles

  • Ch 11 · Files, Environment, Output, and Logs: tmp_path, monkeypatch, capsys, and caplog
  • Ch 12 · Mocking with unittest.mock: Mock, patch, and autospec
  • Ch 13 · When Not to Mock: Fakes, Stubs, and Dependency Injection
  • Ch 14 · Testing Time and Randomness
  • Ch 15 · Testing HTTP Code with httpx MockTransport

Part IV — Beyond Unit Tests

  • Ch 16 · Database Tests with SQLite and SQLAlchemy
  • Ch 17 · Property-Based Testing with Hypothesis
  • Ch 18 · Async Tests with pytest-asyncio
  • Ch 19 · Testing Command-Line Programs

Part V — Quality, Tooling, and Workflow

  • Ch 20 · Coverage: Measuring What Your Tests Exercise
  • Ch 21 · Configuring pytest: pyproject.toml, Plugins, and Hooks
  • Ch 22 · Flaky Tests and Fast Suites: Isolation, Ordering, and Speed
  • Ch 23 · Test-Driven Development: A Walkthrough
  • Ch 24 · Type Checking and Linting Alongside Tests: mypy and ruff

Part VI — Test Suites in Practice

  • Ch 25 · Continuous Integration with GitHub Actions
  • Ch 26 · Maintaining a Test Suite: Smells, Refactoring, and Legacy Code
  • Ch 27 · Integration and End-to-End Tests: Wiring the Whole Checkout
  • Ch 28 · Case Study: The Complete Tallowbrook Test Suite

Appendices
A · pytest Cheat Sheet
B · Templates and Reference
C · Test Suite Production Checklist
D · Glossary

Book details

Detail
Pages513
FormatPDF (A4)
LanguageEnglish
LevelBeginner to Intermediate
Edition1st Edition (2026)
Download on this siteFree sample, first 20 pages (PDF)
Publisherthenexbooks

Files you get

PDF
Testing-Python-with-pytest-Free-Sample.pdf
579.1 KB · version 1 · updated 2 Jun 2026

No reviews yet. Only buyers can review.

More from thenexbooks

Visit store
$9.00