Skip to content
Start free

Test the report on a month you already know

Run the report on September, whose answer finance confirmed, and check every region plus a case that should find nothing.

2 parts · 10 and 3 min · part 1 on a computer

Free course. Your Maestro, an AI, .

When you change a or add new checks, you need a fast way to verify that its math still works. A does this by feeding the code an input whose exact answer you already confirmed and checking that the output matches.

You cannot use the new month that just arrived in your inbox for a known-answer test, because you do not independently know the right totals for that file yet. Instead, you test against a closed month that finance already confirmed. For September (data/sales_2026-09.csv), finance closed the month at 3,000 across eight orders: 1,200 in North, 800 in South, 650 in East, and 350 in West.

Check every part of the known answer

In test_known.py, calling report.run("2026-09") writes the monthly output to reports/history.csv. The test reads September's rows back into a dictionary called by_region and checks North with Python's assert statement: assert by_region["North"] == 1200, by_region.

When the condition after assert is true, Python moves quietly to the next line. When the condition is false, Python stops immediately with an AssertionError and prints by_region so you can see the actual numbers.

Checking North alone, or checking only the grand total of 3,000, leaves room for silent bugs. If one region were too high and another were too low by the same amount, the grand total would still equal 3,000. A complete test checks all four regions individually and checks their sum.

It also checks an that should find nothing. In report.py, the totals dictionary is built by looping over the rows present in the CSV. A region with no orders in September, such as Central, must not appear in reports/history.csv as 0.00 or as a blank row. Write a check that it stays out of the output altogether. If you want to review how assert or not in works on a dictionary, ask your Maestro.

Try it

Run test_known.py once to see the check for North pass. Then add assert lines for South (800), East (650), West (350), the total across regions (3000), and the absence of Central, and finish by printing by_region and "All known-answer checks passed".

Your turn

Run test_known.py. Add asserts for South, East, West and the total, and one that checks a region with no sales (Central) does not appear. You're done when the script prints that every known-answer check passed.

A good place to stop. Up next: part 2 of 2 · 3 min

What to look for in the output

When all six assertions hold, your script prints North matches the known answer, then {'East': 650.0, 'North': 1200.0, 'South': 800.0, 'West': 350.0}, and finally All known-answer checks passed. If any regional figure differs or if "Central" shows up as a key, Python stops at that line and prints by_region. If an assertion fails or the output does not match, ask your Maestro to check your lines in test_known.py.

What a known-answer test cannot tell you

A passing run of test_known.py proves that report.py calculates regional totals, sums the month, and leaves out regions with no orders when given a valid file whose answer you know.

It says nothing about whether next month's incoming file is valid. September's file was clean, whereas November's file (data/sales_2026-11.csv) arrived with a repeated order_id, a negative amount, a blank region, a comma inside an amount, and a date from October. A known-answer test checks the code; checking a new file requires tests that inspect the input rows themselves, which you will write on the next page.

Check yourself: You finish test_known.py for September and all six assertions pass. What have you proved so far?