Course outline · 0% complete

0/29 lessons0%

Course overview →

Table-Driven Tests

lesson 3-3 · ~10 min · 9/29

One loop, many cases

By now each behavior wants several probes, and writing a full test function per probe gets repetitive. The table-driven pattern fixes that with tools you have had since Python for Beginners: a list of tuples and a loop.

  1. build a cases list of (input, expected) tuples
  2. loop over it
  3. assert with a message that names the failing case, lesson 2-3 style

Adding a new edge case is now a one-line change to the table, so the checklist from lesson 3-1 becomes cheap to apply exhaustively. Professional frameworks have the same idea built in, for example @pytest.mark.parametrize, which turns each table row into its own reported test.

Code exercise · python

Run this table-driven suite for sign. Five probes, one loop, and the assert message names the exact failing case if one ever breaks.

Quiz

Your table-driven suite has 12 cases and you discover a new edge case. What does adding it cost?

Code exercise · python

Your turn. The third table row, a name with a double space, crashes initials with an IndexError. Run to confirm, then fix initials so all three cases pass. Python's split() with no argument treats any run of spaces as one separator.

Code exercise · python

Your turn to feel how cheap table rows are. can_vote has one lonely typical case. Add four rows: the boundary trio from lesson 3-2 (17 rejected, 18 accepted, 19 accepted) and the zero case (0 rejected). Match the expected output's order.