Syllabus Lesson 112 of 141 · Invent your tools
Invent your tools

A test idea finder

Why it matters Most missed tests are the same few: empty, zero, negative, None, left out. A tool that reads a signature and lists them is a checklist you cannot forget to run.

Your team wants a helper for code review: paste a function's def line, get the edge cases its parameters suggest. The rules come from the team's testing guide.

edge_cases("def paginate(items: list[str], page: int, size: int = 20) -> list[str]:")
# items: empty, items: one item, items: many items,
# page: 0, page: negative, page: very large,
# size: 0, size: negative, size: very large, size: left out

Real signatures are not that tidy. They span several lines, have nested types like dict[str, list[int]], defaults like sep: str = ", ", a bare * or /, *args, **kwargs, async def, and return types with commas in them.

Your turn

Write edge_cases(signature): signature is a complete def or async def header ending in :, possibly over several lines. Return the list of ideas, each "name: idea", order not important. Skip self, cls, * and /. By annotation:

  • int: 0, negative, very large. float: 0.0, negative, NaN. str: empty, only spaces, non-ASCII. bool: True, False.
  • list, tuple, set, dict (with or without [...]): empty, one item, many items.
  • X | None, None | X or Optional[X]: None, plus the ideas for X. Any other union, and any other type: typical value.
  • No annotation: None, wrong type.
Also add "name: left out" for a parameter with a default, "args: no extra arguments" for *args and "kw: no keyword arguments" for **kw (with their own names). Trap to beat: eleven hidden signatures with every feature listed above.

Spotted a problem in this lesson? Report it · Ask about this lesson

This exercise opens soon

Finish the lesson before it and this one unlocks. The whole course is free, no account needed, and you can read this lesson right now.