Write Your First pytest Test in 10 Minutes
Install pytest, test a small mobile money fee function, run your tests and read what pytest tells you when one fails, with real output at every step.

You wrote a function, ran it a few times, and it seemed to work. Then you changed one line and something else broke without you noticing. Tests catch that for you, every time, in under a second. In Python, the easiest way to write them is pytest.
In this guide you'll install pytest, write your first tests for a small mobile money function, run them, and read what pytest tells you when something goes wrong. It takes about ten minutes.
Step 1: Install pytest
You need Python 3 on your computer. (No Python yet? Follow How to Install Python on Your Computer first.) In a terminal, inside your project's virtual environment, run:
python -m pip install pytest
Step 2: Write some code to test
Make a folder called momo and a file fees.py in it. This function works out a mobile money transfer fee: 1% of the amount, but never less than GHS 0.50 and never more than GHS 10:
def transfer_fee(amount):
"""Mobile money fee: 1% of the amount, at least GHS 0.50, at most GHS 10."""
if amount <= 0:
raise ValueError("amount must be positive")
fee = amount * 0.01
return round(min(max(fee, 0.50), 10.00), 2)
Step 3: Write your first tests
In the same folder, create test_fees.py. pytest finds files whose names start with test_, and runs every function inside whose name starts with test_. Each test uses a plain assert:
import pytest
from fees import transfer_fee
def test_fee_is_one_percent():
assert transfer_fee(200) == 2.00
def test_small_amounts_pay_the_minimum():
assert transfer_fee(20) == 0.50
def test_big_amounts_pay_the_maximum():
assert transfer_fee(5000) == 10.00
def test_zero_is_rejected():
with pytest.raises(ValueError):
transfer_fee(0)
Notice what these tests check: a normal amount, both edges (the minimum and the maximum), and bad input. That mix catches most real bugs. The last test uses pytest.raises to say "this call should raise a ValueError".
Step 4: Run them
From inside the momo folder, type pytest:
pytest
Output:
============================= test session starts =============================
platform win32 -- Python 3.14.4, pytest-9.2.0, pluggy-1.7.0
rootdir: C:\Users\you\momo
collected 4 items
test_fees.py .... [100%]
============================== 4 passed in 0.08s ==============================
Each dot is a passing test. Your Python version, folder and timing will differ a little. Add -v to see each test by name:
test_fees.py::test_fee_is_one_percent PASSED [ 25%]
test_fees.py::test_small_amounts_pay_the_minimum PASSED [ 50%]
test_fees.py::test_big_amounts_pay_the_maximum PASSED [ 75%]
test_fees.py::test_zero_is_rejected PASSED [100%]
Step 5: Watch a test catch a bug
Now break the function on purpose. In fees.py, change amount * 0.01 to amount * 0.1 (10% instead of 1%), and run pytest again:
test_fees.py FF.. [100%]
================================== FAILURES ===================================
___________________________ test_fee_is_one_percent ___________________________
def test_fee_is_one_percent():
> assert transfer_fee(200) == 2.00
E assert 10.0 == 2.0
E + where 10.0 = transfer_fee(200)
test_fees.py:7: AssertionError
_____________________ test_small_amounts_pay_the_minimum ______________________
def test_small_amounts_pay_the_minimum():
> assert transfer_fee(20) == 0.50
E assert 2.0 == 0.5
E + where 2.0 = transfer_fee(20)
test_fees.py:11: AssertionError
=========================== short test summary info ===========================
FAILED test_fees.py::test_fee_is_one_percent - assert 10.0 == 2.0
FAILED test_fees.py::test_small_amounts_pay_the_minimum - assert 2.0 == 0.5
========================= 2 failed, 2 passed in 0.33s =========================
Read it from the top. The Fs show two failures. For each one, pytest shows the test, marks the failing line with >, and the E lines explain it: transfer_fee(200) gave 10.0 when the test expected 2.0. You didn't write any special message; pytest works out these details from your plain assert.
Notice that the maximum and zero tests still pass. The bug only shows on normal and small amounts, which is exactly why testing several cases matters. Change the line back and everything is green again.
Handy options to remember
pytest -v: list every test by name.pytest -x: stop at the first failure.pytest -k minimum: run only tests whose names contain "minimum".pytest test_fees.py::test_zero_is_rejected: run one single test.
Where to go next
You've got the core idea: small functions, plain asserts, and a command that checks everything in a second. From here, pytest grows with you: parametrize runs one test over many inputs, fixtures share setup between tests, pytest.approx compares decimals safely, and mocking replaces slow or external things like an SMS service.
- Testing Python with pytest covers all of that step by step, from your first test to coverage, test-driven development and running tests in CI, with a mobile money wallet as the running example.
- Python Beyond the Basics includes Python's built-in
unittestand the habits that make code easy to test.
New to Python? Start with the Python Crash Course, which runs entirely in your browser.
Comments
Log in to join the conversation.
Loading comments...


