Pages

Topics

Tools

↑↓navigate↵openescclose

Python guides

Every published Python guide, newest first, with practical examples you can copy into your own project.

3,965 guides across 14 topics

Topics

Latest Python guides

Illustration of a custom Hypothesis strategy generating test data for a pytest test, showing a composite of data shapes and a test runner.
Python

Python Hypothesis Custom Strategies with Pytest

Learn to define custom Hypothesis strategies for property-based testing and use them with pytest, covering @composite, .map, and .filter.

Hypothesispytestproperty-based testingcustom strategies
Illustration of Python Hypothesis strategies generating numbers, strings, and lists for property-based testing.
Python

Python Hypothesis Strategies for Numbers, Strings, and Lists

python hypothesis strategies for numbers strings and lists: Use Hypothesis strategies for numbers, strings, and lists to generate diverse test inputs and catch edge ca...

HypothesisProperty-Based TestingPython TestingData Generation
Illustration of property-based testing with Python Hypothesis showing generated inputs and a shrinking failure.
Python

Python Hypothesis Property-Based Testing Basics

Learn how to write property-based tests in Python with Hypothesis: describe invariants, generate inputs with strategies, build composite data, and debug failures through shrinking.

property-based testinghypothesispython testingtest strategies
A stylized diagram showing a pytest test intercepting an HTTP request with a mock response, with a network cable cut symbolizing no real API call.
Python

Mocking API Requests in pytest with Python

Learn how to mock API requests in pytest using unittest.mock and monkeypatch. Simulate responses and errors, verify request arguments, and keep tests fast and deterministic.

pytestmockingrequestsunit testing
An illustration of a Python unit test replacing a function with a spy that records calls while the original logic continues to run.
Python

Python Pytest Mocker: Patch, Spy, and Call Assertions

Use pytest-mock's mocker fixture to patch dependencies, spy on real functions, and assert calls reliably in Python unit tests.

pytestpytest-mockunit testingmocking
Diagram showing a Python pytest mock replacing a real function with a controllable return value and side effect.
Python

Python Pytest Mock: Functions, Classes, Return Values, Side Effects

Learn to mock functions and classes in Python pytest with unittest.mock: set return values, define side effects, patch classes, verify calls, and test async code.

pytestmockingunit testingunittest.mock
A visual representation of code coverage measurement with pytest-cov showing a progress bar and coverage percentage.
Python

Python Pytest Coverage with pytest-cov

Measure Python code coverage with pytest-cov: set thresholds, exclude selected code, and generate terminal, HTML, and XML reports.

pytestcode coveragepytest-covtesting
A pytest command line filtering tests with -k and -m options, showing a focused set of test files.
Python

Python Pytest: Run Specific Tests and Filters

Learn how to run specific pytest tests with node IDs, -k filters, markers, and combined selection to keep test runs focused.

pytesttest selectionpython testingcommand-line
Illustration of pytest running async test functions on an asyncio event loop with await points.
Python

Python pytest asyncio: Writing Async Tests

Learn to write and run async tests with pytest and asyncio: install pytest-asyncio, use async fixtures and markers, control event loop scope, and avoid common failure modes.

pytestasynciopytest-asyncioasync testing
Diagram comparing xunit setup and teardown methods with pytest fixture lifecycle, showing setup, test, and teardown phases.
Python

Python pytest setup/teardown vs fixtures: Which should you use?

Python pytest setup/teardown vs fixtures: Compare xunit-style setup/teardown methods with pytest fixtures to decide which approach improves test isolation, sharing, and maintainability.

pytestfixturessetup teardownpython testing