Set up unit tests, fixtures, parameterization, and API tests with pytest. The most practical testing tool for keeping a Python project in good shape. Rather than listing concepts, this guide is organized so you can follow the content in the order you have to make decisions on a real project.
Key perspective
Backend / systems development
Before syntax, pin down the boundaries a request crosses: validation, processing, storage, and response.
Unit testsAPI testsRegression preventionCI automation
Architecture diagrams
To make what you read stick, it helps to capture the flow as a picture first. The two diagrams below are reference maps you can keep coming back to while learning pytest.
Learning flow
Rendering diagramβ¦
Architecture view
Rendering diagramβ¦
What is pytest?
When you first open pytest, the big picture matters more than individual commands. This section starts with the problems the concepts ahead were created to solve.
pytest is the most widely used test framework in Python. With concise asserts, fixtures, parameterization, and a plugin ecosystem, it tests everything from small functions to API servers.
Feature
Description
assert
Verifies expected values with the plain Python assert.
fixture
Reuses test setup/teardown logic.
parametrize
Repeats one test over several inputs.
plugins
Extend it with coverage, mock, and async testing.
Basic tests
Here you look at Basic tests alongside real code. Rather than copying the example as-is, read it with an eye on the inputs, the outputs, and the parts most likely to change.
Here you look at Fixture alongside real code. Rather than copying the example as-is, read it with an eye on the inputs, the outputs, and the parts most likely to change.
A fixture supplies the setup data that tests repeat, one function at a time.
Here you look at Parametrize alongside real code. Rather than copying the example as-is, read it with an eye on the inputs, the outputs, and the parts most likely to change.
Verify several inputs and expected values with a single test function.
Here you look at Testing FastAPI alongside real code. Rather than copying the example as-is, read it with an eye on the inputs, the outputs, and the parts most likely to change.
A FastAPI app can be verified as if by HTTP requests with TestClient.
pytest practical design is a point where the options diverge. Using the table to compare what each approach is for and how it differs in operation makes later decisions much easier.
With pytest, designing fixture scope and the test data lifecycle well matters more than creating many test files.
Decision point
Question to ask
Practical guideline
Boundaries
Which parts of the pytest code are most likely to change?
Separate input/output, configuration, external integrations, and the core rules.
State
Where is state created and where does it go away?
Make the owner and lifecycle of state visible in the code.
Failure
What does the caller receive on failure?
Decide the timeout, fallback, and error contract first.
pytest operating standards
This section covers pytest operating standards from a practical perspective. Instead of memorizing the concept, focus on the situations in which you would reach for it.
In CI, make separating slow tests, isolating flaky tests, parallel execution, and a coverage threshold your operating standards.
pytest verification strategy
pytest verification strategy is a point where the options diverge. Using the table to compare what each approach is for and how it differs in operation makes later decisions much easier.
A good test has a failure message that explains the cause, and mocks should be used only at external boundaries.
Quality axis
How to verify
Definition of done
Correctness
Automate the normal/failure cases.
The core scenarios pass reproducibly.
Regression prevention
When fixing a bug, leave the same case behind as a test.
The same failure is not deployed again.
Operability
Check the logs, metrics, and alerts.
There is a path for tracing the cause when a problem occurs.
pytest: frequently asked questions
How is pytest different from unittest?
unittest is the testing tool in the standard library, written by subclassing a class, while pytest lets you write tests as plain functions with assert statements. pytest shows the compared values in detail on failure and has a rich set of fixtures and plugins. Tests written for unittest also run under pytest.
What is a fixture and when do I use one?
A fixture is a function that prepares what a test needs. Name the fixture as an argument of the test function and pytest builds it and passes it in before the test runs. Use fixtures for setup and cleanup shared across tests, such as database connections, temporary files, and test clients.
How do I test code that calls an external API?
In unit tests, do not make the real call; replace the call with a fake that returns a fixed response. Simulate timeouts and error responses as well as successes, and check how the code handles them. Verify that the real integration still works with a separate, small set of integration tests.