AI for Tests, Docs and Debugging

Updated · 6 min read
mediumPython 3.13aitestingdebuggingdocumentationdevelopers

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:

Python
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}")
Output
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
One failing test finds a real bug

// 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:

Python
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")
Output
0 0 PASS
7 0 PASS
8 5 PASS
21 10 PASS
100 40 PASS

Docs and Debugging

TaskHow AI helpsWhat you check
DocumentationDrafts docstrings, READMEs and comments from the codeThat they describe what the code really does
Explaining errorsTurns a stack trace into plain words and likely causesReproduce the error before trusting the cause
DebuggingSuggests what might be wrongConfirm with a failing test, then fix
More testsSuggests edge cases you didn't think ofEach 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:

  1. What was the bug in late_fee, and why did // cause it?
  2. Why must a person check the expected values?
  3. What should you ask for before asking for a fix?

Quick check: AI for tests, docs and debugging

4 questions
1What did late_fee(8) return before the fix?
2Who should check the expected value in each test?
3A test fails. What should you usually change?
4What helps most before fixing a bug?

Key Takeaways

Quick reference

TaskRule
TestsCheck every expected value
DocsRead them against the code
ErrorsReproduce before trusting a cause
FixesFailing test first, then fix

Was this tutorial helpful?

Lesson 72 of 186