AI for Tests, Docs and Debugging
What's the fastest way to find a bug you don't know exists? Campus Helper has a small function for late fees, and everyone assumes it's right. Advitha asks an AI assistant to suggest tests for it, checks each expected value against the school's written policy, and runs them. One fails. The bug had been there all along.
This lesson shows how AI helps with tests, documentation and debugging, and why a person must check the expected answers.
Tests That Catch a Real Bug
The school's policy: $5 for each week or part of a week late, after a 7-day grace period, capped at $40. Here is the function, with five tests an assistant suggested:
def late_fee(days_late):
"""$5 for each week or part of a week late, after a 7-day grace period, capped at $40."""
if days_late <= 7:
return 0
weeks = (days_late - 7) // 7
return min(weeks * 5, 40)
# Tests an AI assistant suggested; a person checked each expected value against the policy
tests = [
(0, 0, "on time"),
(7, 0, "last day of grace period"),
(8, 5, "one day into the first late week"),
(21, 10, "two full weeks late"),
(100, 40, "the $40 cap"),
]
for days, expected, name in tests:
got = late_fee(days)
status = "PASS" if got == expected else "FAIL"
print(f"{status} {name}: late_fee({days}) = {got}, expected {expected}")PASS on time: late_fee(0) = 0, expected 0
PASS last day of grace period: late_fee(7) = 0, expected 0
FAIL one day into the first late week: late_fee(8) = 0, expected 5
PASS two full weeks late: late_fee(21) = 10, expected 10
PASS the $40 cap: late_fee(100) = 40, expected 40// rounds down, so one day into the first late week counts as zero weeks. The policy says part of a week counts, so the fix rounds up, by adding 6 before dividing:
def late_fee(days_late):
"""$5 for each week or part of a week late, after a 7-day grace period, capped at $40."""
if days_late <= 7:
return 0
weeks = (days_late - 7 + 6) // 7 # round partial weeks up
return min(weeks * 5, 40)
for days, expected in [(0, 0), (7, 0), (8, 5), (21, 10), (100, 40)]:
print(days, late_fee(days), "PASS" if late_fee(days) == expected else "FAIL")0 0 PASS
7 0 PASS
8 5 PASS
21 10 PASS
100 40 PASSDocs and Debugging
| Task | How AI helps | What you check |
|---|---|---|
| Documentation | Drafts docstrings, READMEs and comments from the code | That they describe what the code really does |
| Explaining errors | Turns a stack trace into plain words and likely causes | Reproduce the error before trusting the cause |
| Debugging | Suggests what might be wrong | Confirm with a failing test, then fix |
| More tests | Suggests edge cases you didn't think of | Each expected value |
Common Mistakes
Accepting AI-written expected values. Check each against the requirement; the AI may copy the bug.
Letting AI "fix" tests to make them pass. Fix the code, not the test, unless the test was wrong.
Trusting generated docs without reading them. Wrong documentation misleads every future reader.
Interview Corner
How can AI help with testing?
By suggesting test cases, especially edge cases, and drafting test code. A developer must check every expected value against the actual requirement, so the tests catch bugs instead of confirming them.
How would you use AI to debug a problem?
Ask it to explain the error and suggest causes, then write a failing test that reproduces the bug, fix the code, and confirm the test passes, without letting it weaken the tests.
Practice
Exercise: explain, test, find a bug. Pick a small function you didn't write. Ask an AI to explain it, then to suggest five tests including edge cases. Check every expected value yourself, and run them. Did you find a real bug? If not, add one deliberately and see whether the tests catch it.
Check your understanding:
- What was the bug in
late_fee, and why did//cause it? - Why must a person check the expected values?
- What should you ask for before asking for a fix?
Key Takeaways
Quick reference
| Task | Rule |
|---|---|
| Tests | Check every expected value |
| Docs | Read them against the code |
| Errors | Reproduce before trusting a cause |
| Fixes | Failing test first, then fix |
Was this tutorial helpful?