pytest
A popular Python testing framework that finds test functions automatically and reports failures with detailed comparisons.
In more detail
Tests are plain functions whose names start with test_, in files named like test_*.py, using ordinary assert statements, with no base class or registration needed. Running pytest discovers and runs them all, and it rewrites assert statements so failures show the values involved. It also adds fixtures, markers and parametrization, and is a third-party package installed with pip.
Examples
python -m pip install pytest
pytest -qpytest -q test_cart.pyRelated terms
- unit test — An automated check of one small piece of code, such as a single function, that runs quickly and on its own.
- assert statement — A statement, assert condition, that does nothing if the condition is true and raises AssertionError if it is false.
- test discovery — How a test runner finds tests without being told: pytest collects files named test_*.py and, inside them, functions named test_*.
- arrange, act, assert — A three-step shape for a test: set up the starting state, perform the one behaviour under test, then check the observable result.
- test collection — The step where pytest searches files and names to decide which tests exist before running any of them.
- fixture — A named piece of setup, defined with @pytest.fixture, that a test receives by listing the fixture's name as a parameter.
- assertion rewriting — pytest's trick of rewriting plain assert statements as it imports test files so failures print the values involved.
- marker — A label attached to a test with @pytest.mark.name so you can select, skip or configure tests by that label.
Taught in
- Prompts As Code — AI Engineering: LLM Apps, RAG & Agents
- Measuring Retrieval — AI Engineering: LLM Apps, RAG & Agents
- Connect An Agent To MCP — AI Engineering: LLM Apps, RAG & Agents
- Eval-Driven Development — AI Engineering: LLM Apps, RAG & Agents
- Evaluating RAG And Agents — AI Engineering: LLM Apps, RAG & Agents
- Regression Tests In CI — AI Engineering: LLM Apps, RAG & Agents
- A Testing Strategy In The Pipeline — Microsoft Azure DevOps Engineer Expert (AZ-400) Exam Prep
- An Hour Without Payments: Continuously Reconciled — Certified GitOps Associate (CGOA) Exam Prep
- Seven Green Tests And A Bug That Shipped — ISTQB Certified Tester Foundation Level (CTFL v4.0) Exam Prep
- Test First, DevOps And Shift Left — ISTQB Certified Tester Foundation Level (CTFL v4.0) Exam Prep
- Equivalence Partitioning — ISTQB Certified Tester Foundation Level (CTFL v4.0) Exam Prep
- Boundary Value Analysis: Two Values And Three — ISTQB Certified Tester Foundation Level (CTFL v4.0) Exam Prep
- Acceptance Test-Driven Development — ISTQB Certified Tester Foundation Level (CTFL v4.0) Exam Prep
- Test Tools And The Truth About Automation — ISTQB Certified Tester Foundation Level (CTFL v4.0) Exam Prep
- Testing With pytest — Hands-on Python: Complete Video Course & Book
- The Cost And Value Of A Test — Automated Testing, TDD & Quality Engineering
- Introducing pytest — Automated Testing, TDD & Quality Engineering
- Parametrised Tests — Automated Testing, TDD & Quality Engineering
- Fixtures And Their Scope — Automated Testing, TDD & Quality Engineering
- Naming Tests For Readability — Automated Testing, TDD & Quality Engineering
- Making Failures Explain Themselves — Automated Testing, TDD & Quality Engineering