To start a pytest tutorial from zero: create a virtual environment, run pip install pytest, write a file named test_something.py with a function whose name starts with test_, and run pytest. A test passes when its assert statements hold and no exception is raised. This guide uses pytest 8 on Python 3.13, and everything also works on Python 3.12.
pytest is the most widely used Python testing framework because tests are plain functions with plain assert statements, with no classes or special assertion methods to learn. You will install it, write a small module, test it, read the output and run a single test.
Always install tools per project so versions do not clash. In a new project folder:
The last command prints something like pytest 8.x.x. Running python -m pip instead of bare pip guarantees you install into the Python you just activated.
To make the setup repeatable, record the dependency in a file:
Modern projects usually declare it in pyproject.toml under an optional dependency group, but requirements-dev.txt is fine for learning.
Create this layout:
Put the code under test in calculator.py:
Keeping production code and tests in separate files means the tests can import the module the same way any user would.
Create test_calculator.py in the same folder:
Each test follows the arrange, act, assert pattern: set up the input, call the code, check the result. Names should describe the behaviour, so a failing test name tells you what broke before you open the file.
Run all tests from the project folder:
Each dot is a passing test. The letters you will see in other runs are F for a failure, E for an error and s for a skipped test.
Use pytest.raises as a context manager. The test passes only if the block raises the given exception type, and match checks the message with a regular expression:
For floating point results, do not compare with == after arithmetic that rounds. Use pytest.approx:
Change the first test to assert add(2, 3) == 6 and run pytest again. You will see output similar to this:
The > line marks the failing statement and the E lines show the real values. This detail comes from pytest rewriting your assert statements when it imports test modules, which is why you never need assertEqual.
As the suite grows, you rarely run everything while developing. These options cover most needs:
| Command | What it runs |
|---|---|
pytest test_calculator.py | One file |
pytest test_calculator.py::test_add_two_numbers | One test by node ID |
pytest -k divide | Tests whose name contains divide |
pytest -x | Stops at the first failure |
pytest --lf | Only the tests that failed last time |
pytest -v | One line per test with its name |
pytest -q | Quiet output |
You can also start pytest with python -m pytest, which adds the current directory to sys.path; this is a handy fix when imports fail in a simple project layout.
No. Plain functions are the normal style. Classes are optional and only useful for grouping related tests; a class name must start with Test and must not define __init__.
By default pytest collects files named test_*.py or *_test.py, and inside them functions named test_*. A file called calculator_tests.py or a function called check_add is silently ignored.
unittest is in the standard library and uses classes and methods such as assertEqual. pytest uses plain assert, has fixtures and parametrization built in, and can also run existing unittest test cases.
Run pytest -x, or pytest --maxfail=3 to stop after three failures.
Install pytest into a virtual environment with python -m pip install pytest, name files test_*.py and functions test_*, and use plain assert for checks. Use pytest.raises for exceptions, pytest.approx for floats, and options such as -k, -x and --lf to run only what you need.