Exercise: Test the Food Truck Report

Write the pytest tests for the Greenway Food Truck's working report functions, from the total of each sale and the week's numbers to the location table, the saved file and the chart, and prove they're good by catching four bugs the check plants in copies of the code.

Journeyman · Python · Updated

By

The Greenway Food Truck's weekly report works today. It works out the total of every sale, the week's numbers, a table for each location and a chart of the items sold, and the owner plans the next week from it. Nobody wrote tests for it, though, so the next person who changes it could put a wrong number in front of the owner and nobody would know. This time the code is finished and the tests are yours to write. And to show your tests are worth having, a check plants four real bugs in copies of the code, one at a time, and tells you whether your tests caught each one.

Get the exercise code

Open this lesson's GitHub repository, github.com/ask-codey/exercise-data-test-food-truck-report, also linked from the Exercise box beside the lesson. Clone it into your python-course folder. In VS Code's terminal:

cd ~/python-course
git clone https://github.com/ask-codey/exercise-data-test-food-truck-report.git

Git makes a folder, exercise-data-test-food-truck-report, with the exercise code inside. Get the Exercise Code with Git walks through cloning step by step, if you'd like a reminder. Open that folder in VS Code (File > Open Folder), open its terminal with Terminal > New Terminal, and make the exercise's virtual environment, the same three steps as in the exercises before. On Windows (PowerShell):

python -m venv .venv
.venv\Scripts\Activate.ps1
pip install -r requirements.txt

On a Mac (zsh):

python3 -m venv .venv
source .venv/bin/activate
pip install -r requirements.txt

The prompt starts with (.venv) once the virtual environment is on. Then make the data, once:

python seed.py
Wrote data/incoming/week-2026-09-07.csv: 409 sales.
Wrote data/incoming/week-2026-09-14.csv: 422 sales.
Wrote data/incoming/week-2026-09-21.csv: 412 sales.
Wrote data/incoming/week-2026-09-28.csv: 409 sales.
Wrote data/incoming/week-2026-10-05.csv: 411 sales.
Wrote data/incoming/week-2026-10-12.csv: 403 sales.
Wrote data/incoming/week-2026-10-19.csv: 416 sales.
Wrote data/incoming/week-2026-10-26.csv: 390 sales.

The folder holds:

  • report_functions.py: the report's five functions, with a docstring each: the functions from Exercise: Food Truck Weekly Report, made sturdier for a missing price and an empty week, and two more for a table of the locations. It works: don't change it.
  • seed.py: makes the same eight weeks of the truck's sales as the last exercise, our own made-up data, with a fixed random seed so everyone gets the same rows.
  • report.py: prints the newest week's numbers, so you can see the functions at work on real data.
  • tests/test_report_functions.py: your tests. It has one worked example.
  • tests/conftest.py: sets matplotlib's Agg backend, so the chart tests draw with no window.
  • check.py and check/: the check, with its four planted-bug copies of report_functions.py. You don't need to change them, and it's more fun not to read the copies until you're done.
  • README.md: the rules, the two small weeks to test on, and the list of behaviors your tests should cover.
  • requirements.txt and pytest.ini: the packages the exercise needs, and where pytest finds the tests and the code.

What to do

The rules your tests protect, for a table of sales with the columns location, item, quantity and price:

Function What it does
add_total(df) Returns a new table with a total column, quantity times price. A sale with no price has a missing total (NaN), not 0.0.
summarize(df) Returns a dictionary: sales (the sum of total), items_sold, unpriced (sales with no price), best_item (the most sold, not the most sales) and best_location (the largest sales). An empty week gives zeros and None.
sales_by_location(df) Returns a new table with one row per location, the largest sales first: location, items_sold, sales.
save_location_table(df, path) Writes that table to a CSV file at path.
items_chart(df) Returns (fig, ax): a bar per item, the most sold first, titled Items sold this week.

Every function leaves the table it's given unchanged. The worked example in tests/test_report_functions.py shows the shape of a test:

def test_1_add_total_2_street_tacos_at_9_00_is_18_00():
    sales = pd.DataFrame({"location": ["City Park"], "item": ["Street Tacos"], "quantity": [2], "price": [9.00]})
    result = add_total(sales)
    assert result["total"].tolist() == [18.0]
  1. Read the README's two small weeks and its list of behaviors. There are twelve, numbered, each with the exact table, number or text to expect, worked out from a four-sale week and a week with a missing price. Behavior 1 is the worked example.
  2. Build the small weeks in your test file, each in a function that returns a fresh DataFrame, so every test starts from the same rows.
  3. Test add_total: the whole four-sale table with assert_frame_equal, the missing price, and that the table it was given is unchanged (behaviors 2 to 4).
  4. Test summarize: the four-sale week's dictionary, the best item, the missing price and the empty week (behaviors 5 to 8).
  5. Test the location table and its file: sales_by_location with assert_frame_equal, and save_location_table writing into tmp_path (behaviors 9 and 10).
  6. Test the chart: its title and labels, and its bars' heights and names, closing each figure with plt.close(fig) (behaviors 11 and 12).

Name each test after its behavior's number and what it checks, such as test_7_summarize_a_week_with_a_missing_price.

Check your work

While you write, run your tests:

python -m pytest

Every test you write should pass, because the code works. If one fails, work out its expected value again by hand from the README: the test is the part to fix.

Then see how good your tests are:

python check.py

It runs your tests five times: once with the working report_functions.py, then once with each of four copies that has a bug planted in it. One copy picks the best item by the number of sales, one counts a missing price as 0, one has add_total change the table it was given, and one puts the chart's bars in alphabetical order. A bug counts as caught when at least one of your tests fails on its copy. At the start, check 1 passes (the worked example passes) and checks 2 to 5 don't: that's expected, because one test can't catch four bugs yet. A check that isn't passing yet says what kind of test it's missing, for example Check every bar's height, in order: 5, 3, 2 for the four-sale week.

In your textbook, planting small bugs in working code to see whether the tests notice is called mutation testing.

Hints

  • Work out each expected value by hand, from the README's small weeks, as Testing pandas Functions with Small Tables does. A test that copies whatever the code returns proves nothing.
  • A whole table in one check. Build the table you expect and compare with pd.testing.assert_frame_equal(result, expected). If it says the dtypes differ, write 9.0, not 9, for a column of decimals.
  • A missing value isn't equal to anything, itself included. Check one with pd.isna(value).
  • Copy, call, compare. To prove a function leaves its table alone, make before = sales.copy(), call the function, then compare sales with before.
  • A file needs a folder, and tmp_path is a fresh one for each test. path.read_text(encoding="utf-8") reads the file back as one string.
  • Read the chart, don't look at it, as Testing a Chart Without Looking at It does: ax.get_title(), the bars in ax.patches with get_height(), and the names from ax.get_xticklabels().

When you're done

Every test is green with python -m pytest, and python check.py prints:

Checking your tests against the working report_functions.py and four copies with a bug planted in each.

1) your tests pass on the working report_functions.py: yes
2) your tests catch planted bug 1, the best item picked by the number of sales, not the quantity sold: yes
3) your tests catch planted bug 2, a missing price counted as a price of 0: yes
4) your tests catch planted bug 3, add_total changing the table it was given: yes
5) your tests catch planted bug 4, the chart's bars in alphabetical order, not the most sold first: yes

All 5 checks pass: your tests catch every planted bug.

And python report.py shows the functions at work on the week of October 26:

Greenway Food Truck, week-2026-10-26.csv
1) sales: 4455.0
2) items sold: 537
3) sales with no price: 0
4) best item: Street Tacos
5) best location: Stadium

  location  items_sold  sales
   Stadium         210 1730.0
Office Row         206 1706.5
 City Park         121 1018.5

Now open the four copies in check/planted-bugs/ and find each bug. Which of your tests caught bug 2? A test of the week's sales alone doesn't: a missing price and a price of 0 add the same nothing to a sum. And no week so far has a missing price, so the real data would never show the bug either. What does that tell you about checking the count of unpriced sales, or the total column itself, on a small week you made for the purpose?

Submit your exercise

The exercise is how you finish this lesson. When your work is done, put it on GitHub in a repo of your own, then submit that repo's address here. Hand In Your Work with Git walks through each step.

  1. On GitHub, click +, then New repository. Name it (for example exercise-data-test-food-truck-report), choose Public, and click Create repository. Leave out the README: your folder already has one.

  2. In VS Code's terminal, in your exercise folder, send your work to your new repo. Point your copy at your own repo, then commit and push:

    git remote set-url origin https://github.com/your-name/exercise-data-test-food-truck-report.git
    git add .
    git commit -m "My tests"
    git push -u origin main
    
  3. Paste your repo's address, such as https://github.com/your-name/exercise-data-test-food-truck-report, into Submit your exercise on this page. The site checks that the repo is public and holds your commits, and then the lesson is complete.

Changed something after you submitted? Push it, and submit again: your latest submission is the one that counts.

So what

You can now prove an analysis is right, and prove your proof is good: tests on small tables you worked out by hand, on the empty week and the missing price, on the table a function was given and on the chart it draws are the ones that catch a wrong number before the owner acts on it.

Key terms

Related