Python, properly · Chapter 21 of 42

Testing with pytest

By the end of this chapter you can write tests with pytest, run them and read their failures, cover many cases with parametrize, test that code raises the right error, share setup with fixtures, and tell a test that protects you from one that only looks busy. Build it in your browser: a real editor and terminal, AI that explains, and tests that tell you when it works.

  • Part: Code that holds up
  • 4 tasks
  • Python
  • In the all labs plan

The first 2 chapters of Python, properly are free. This one comes with the all labs plan.

Why you need this

Last week someone changed the coupon code, and the free-delivery rule broke with it. Nobody noticed for six days, until Rafiq found customers paying Rs 30 for delivery on Rs 300 orders. Every rule was "checked" when it was written, by running the script once and looking at the output. Nobody looked again after the change.

A test is code that checks code: it calls a function with a known input and compares the result with what the rule says. Tests run in a second, every time, after every change, and they never get bored. Every serious Python project has them, and every AI lab after this one uses pytest for its checks.

What you build: 4 tasks

Each task has tests. You write the code in the lab's editor and press Check.

  1. Task 1

    test_fee.py: the free-delivery boundary

    The rule broke once already: write the tests that would have caught it. In test_fee.py, write pytest tests for billing.py's delivery_fee: Rs 30 for delivery, free when the subtotal is Rs 300 or more.

    The check runs your tests with pytest: they must all pass on the real billing.py, and at least one must fail on each of four broken copies (for example, one that is free only above Rs 300, and one that charges a different fee).

    Run them yourself with pytest test_fee.py. Do not change billing.py.

  2. Task 2

    test_coupons.py: many cases with parametrize

    In test_coupons.py, test billing.py's apply_coupon(total, code) with one test function and @pytest.mark.parametrize, one row per case: (total, code, expected).

    Cover what the docstring promises: no coupon, both as None and as an empty string; DABBA10 on a total whose 10% is not a whole number (the discount is rounded down); FIRST50 on a normal total and on a total under Rs 50 (it stops at 0); and a code written in small letters with spaces around it.

    The check runs your tests on the real billing.py (all pass) and on five broken copies (each must fail at least one case).

  3. Task 3 · interview question

    Interview: test_errors.py, testing that it refuses

    In test_errors.py, test that billing.py refuses bad input, with pytest.raises:

    apply_coupon with an unknown code (like CHAI20) raises a ValueError whose message names that code; line_total(price, 0) raises a ValueError. Also test the nearest allowed value: line_total(120, 1) is 120.

    The check runs your tests on the real billing.py (all pass) and on five broken copies: one that ignores unknown codes, one that raises the wrong type, one whose message leaves the code out, one that accepts a quantity of 0, and one that refuses a quantity of 1.

    Be ready to explain why a test of an error also needs a test of the nearest value that is allowed.

  4. Task 4

    test_receipt.py: a fixture and tmp_path

    In test_receipt.py, test billing.py's save_receipt(folder, order_id, lines) without touching your real folders. Write a fixture (a function marked with @pytest.fixture) that returns a list of receipt lines, and a test that takes it and pytest's tmp_path.

    Check the three things the docstring promises: the file is folder/<order_id>.txt, it holds the lines in order with a newline after each one (the last one too), and save_receipt returns that path.

    The check runs your tests on the real billing.py (all pass) and on three broken copies (each must fail a test).

What you will learn

  • assert and pytest: test files, test functions, running them, and reading a failure.
  • What makes a good test: one behaviour each, edges and boundaries, and a test that can actually fail.
  • @pytest.mark.parametrize: one test, many cases.
  • pytest.raises: testing that bad input is refused.
  • Fixtures: shared setup, and the built-in tmp_path.

More in Code that holds up

Build "Testing with pytest" yourself

DabbaDrop, a Mumbai tiffin startup, runs on Excel and WhatsApp. You are its first Python developer. A real editor and terminal in your browser, AI that explains, and a Check that says when it works.

The first 2 chapters of Python, properly are free. This one comes with the all labs plan.