What pytest is and how it works
pytest is a framework for automated tests in Python. A test is an ordinary test_… function with an assert statement, and fixtures prepare the data and environment for it. pytest finds tests by file and function names and explains every failure in detail.
pytest suits unit, integration and functional tests of Python code, including checks of HTTP APIs. It also runs tests written with unittest, so an old test suite can be migrated gradually. Browser tests are covered in the Playwright reference.
A test has four steps: arrange, act, assert and cleanup. In pytest, fixtures take care of arrange and cleanup, leaving only the act and assert steps in the test body:
import pytest @pytest.fixturedef cart(): # arrange: item, price, quantity return [("book", 100, 2), ("pen", 15, 4)] def test_cart_total(cart): # act total = sum(price * qty for _, price, qty in cart) # assert assert total == 260A run has two stages. First comes collection: pytest walks the directories, opens test_*.py and *_test.py files, and collects test… functions and methods of Test… classes. Then each test goes through three phases: setup prepares the fixtures, call runs the test, and teardown finalises the fixtures. The outcome depends on where the error occurred. A failed assert or any other exception in the test body is failed. An exception in a fixture is error, and if a fixture fails during setup, the test itself does not run.
How it differs from unittest
| Feature | pytest | unittest |
|---|---|---|
| Test | a test_… function or a method of a Test… class, no inheritance | a method of a class that inherits from unittest.TestCase |
| Assertions | plain assert; pytest shows the values of the expression | methods such as self.assertEqual() and self.assertTrue() |
| Setup and cleanup | fixtures; a test requests them by argument name | setUp() and tearDown(), setUpClass() and tearDownClass() |
| Parametrisation | @pytest.mark.parametrize | subTest(); pytest parametrisation does not work in TestCase classes |
| Plugins | plugin fixtures and hooks work in all tests | third-party plugins do not always work in TestCase classes |
Write new tests in the pytest style: functions, assert and fixtures. Run existing unittest tests with pytest as they are. In TestCase classes, the skip, skipif and xfail markers and fixtures with autouse=True work, but regular fixtures and parametrisation do not.
What you need to know first
- Python: functions, modules, imports and the
venvvirtual environment — see the Python reference. - How to choose test values: equivalence classes and boundary values — see the “Test design” reference.
- What to check in HTTP API responses — see the “API testing” reference.
Section sources
the section is written from these pages- Anatomy of a testdocs.pytest.org/en/stable/explanation/anatomy.html(opens in a new tab)
- Good Integration Practicesdocs.pytest.org/en/stable/explanation/goodpractices.html(opens in a new tab)
- How to use unittest-based tests with pytestdocs.pytest.org/en/stable/how-to/unittest.html(opens in a new tab)
- About fixturesdocs.pytest.org/en/stable/explanation/fixtures.html(opens in a new tab)
- API Referencedocs.pytest.org/en/stable/reference/reference.html(opens in a new tab)