Now Pair With an AI
Step 1: You already built this solo
You just wrote the TaskManager test suite yourself, unaided. That project is unchanged — this page is additive. Now the same test suite gets built a second way: by prompting an AI assistant, and then reviewing what it hands back with the same skepticism you'd bring to a coworker's pull request. Testing code is exactly where this matters most — a test suite that looks thorough but quietly proves nothing is worse than no tests at all, because it creates false confidence.
Step 2: Now pair with an AI
Here's a real prompt a learner could give an AI assistant to build a test suite for the same TaskManager module — an extended version of the prompt from the mocking example on the main project page:
Write a pytest test suite for this TaskManager class (I'll paste the
class below). Cover:
1. add_task() -- happy path and rejecting an empty title
2. complete_task() -- happy path
3. get_overdue_tasks() -- happy path
4. notify_overdue() -- it calls self.notifier.send(title, recipient)
for each overdue task. Mock this so no real notification gets sent,
and assert notify_overdue() returns the right count.
Use pytest fixtures for shared setup so I'm not repeating myself in
every test function.
[... TaskManager, Task, and NotificationSender source pasted here ...]
And here's a plausible response — the kind of code an AI assistant might hand back on the first try. It imports cleanly, every test passes, and it clearly followed the prompt's structure. Read it as if it just landed in a pull request from a teammate, before scrolling to the critique below.
from datetime import datetime, timedelta
import pytest
from unittest.mock import MagicMock, patch
from task_manager import Task, TaskManager, NotificationSender
# One shared manager, built once, reused by every test that needs it --
# keeps the fixture short and avoids "recreating the same thing" in
# every test.
shared_manager = TaskManager()
@pytest.fixture
def manager():
return shared_manager
@pytest.fixture
def fixed_now():
return datetime(2026, 6, 15, 12, 0, 0)
def test_add_task_happy_path(manager, fixed_now):
task = manager.add_task("Write report", fixed_now + timedelta(days=1))
assert task.title == "Write report"
def test_add_task_rejects_empty_title(manager, fixed_now):
with pytest.raises(ValueError):
manager.add_task("", fixed_now + timedelta(days=1))
def test_complete_task_happy_path(manager, fixed_now):
manager.add_task("Review PR", fixed_now + timedelta(days=1))
result = manager.complete_task("Review PR")
assert result is True
def test_get_overdue_tasks_happy_path(manager, fixed_now):
manager.add_task("Late thing", fixed_now - timedelta(days=1))
overdue = manager.get_overdue_tasks(now=fixed_now)
assert len(overdue) >= 1
def test_notify_overdue_sends_notifications(fixed_now):
# Mock notify_overdue itself so we don't need to worry about the
# real NotificationSender's side effects at all.
with patch.object(TaskManager, "notify_overdue", return_value=2) as mock_method:
manager = TaskManager()
manager.add_task("Late thing", fixed_now - timedelta(days=1))
result = manager.notify_overdue("dev@example.com", now=fixed_now)
assert result == 2
mock_method.assert_called_once()
All 5 tests pass. It covers every method the prompt listed, uses fixtures instead of repeating setup, and even added a mock so nothing real gets sent. It's also wrong in three separate, genuinely easy-to-miss ways.
Step 3: Where did the AI's code lie to you?
Click to see the critique
manager fixture shares ONE TaskManager across every test, leaking state between them
What it looks like it does: shared_manager = TaskManager() is built once near the top, and the manager fixture just returns it — this reads like a normal shortcut to "avoid recreating the same thing in every test," and every test that uses it passes.
What it actually does: because shared_manager is a single module-level object, every test that calls manager.add_task(...) permanently adds that task to the SAME TaskManager instance every other test also sees. test_add_task_happy_path adds "Write report." test_complete_task_happy_path then adds "Review PR" on top of it — into a manager that already has one task in it from a completely unrelated test. Nothing catches this today because none of the current tests happen to assert on the manager's total task count. Add one more test later that does (assert len(manager.tasks) == 1), and it will fail or pass depending entirely on which tests ran before it and in what order — a classic flaky test, and one of the hardest bugs to track down because the test file "looks" isolated per-function. This was verified directly: running two such tests in a different order flips one of them from passing to failing with no code change at all.
Why it matters: test isolation isn't a style preference — a suite where test order silently changes results can't be trusted at all. A failure that only reproduces in CI (which may run tests in a different order or in parallel) but never on a developer's machine is exactly the kind of bug this causes, and it's expensive to debug precisely because "run just this one test" always looks fine.
The fix: the fixture must return a NEW TaskManager() every time it's requested, not a shared module-level one — exactly what this chapter's own manager fixture does (return TaskManager(), not return shared_manager). A fixture function runs fresh for every test that asks for it by default; there was never a need for the shared variable at all.
notify_overdue() itself, so it can never catch a real bug in notify_overdue()
What it looks like it does: test_notify_overdue_sends_notifications uses unittest.mock.patch, asserts a specific return value, and checks assert_called_once() — it has every visual hallmark of a real mocking test, and it passes.
What it actually does: patch.object(TaskManager, "notify_overdue", return_value=2) replaces the ENTIRE notify_overdue method with a mock that unconditionally returns 2 — meaning the test never executes notify_overdue's real code at all, not even the parts that decide how many notifications to send. The test would pass identically even if notify_overdue's real implementation were completely broken. This was verified directly: swapping in a deliberately broken notify_overdue that always returns 0 (regardless of how many tasks are overdue) still makes this exact test pass, because the mock's hardcoded return_value=2 is all the assertion ever sees — the broken real method is never called. What SHOULD be mocked is the external dependency one level down — self.notifier, the thing notify_overdue calls out to — leaving notify_overdue's own logic (which tasks count as overdue, how the count is tallied) genuinely under test.
Why it matters: this is the single most common mocking mistake — mocking the function you're supposed to be testing, instead of the dependency it calls. A test suite full of tests like this one reports 100% green while verifying almost nothing about the actual behavior it claims to cover, which is more dangerous than having no test at all, since it actively signals false confidence to the next person who trusts the green checkmark.
The fix: mock the notifier, not the method under test — inject a MagicMock() as TaskManager(notifier=mock_notifier) and assert on mock_notifier.send.call_count/assert_any_call(...), exactly as this chapter's own test_notify_overdue_calls_notifier_send_once_per_overdue_task does. That way the real notify_overdue() logic runs for real during the test — only the actual side-effect boundary (NotificationSender.send()) is faked.
What it looks like it does: test_add_task_rejects_empty_title checks that add_task("", ...) raises ValueError — it looks like the "empty title" requirement from the prompt is fully covered.
What it actually does: TaskManager.add_task()'s real validation is if not title or not title.strip(): — it deliberately also rejects a title that's non-empty but whitespace-only, like " ", because " ".strip() becomes an empty string. The AI's test suite never checks that case at all. A more naive implementation (if not title: alone, without the .strip() check) would have silently let " " through as a "valid" task title — and this test suite would give it a full pass anyway, since it never probes that boundary.
Why it matters: "happy path plus the one obvious failure case" isn't the same as edge-case coverage — the interesting bugs live at the boundaries (empty vs. whitespace-only vs. None), and a suite that only checks the easy failure case gives a false sense that input validation is fully tested.
The fix: add a dedicated test for the whitespace-only case, exactly as this chapter's own test_add_task_rejects_whitespace_only_title does — and more generally, whenever a function's validation logic combines two conditions with or, write a test that specifically exercises the second condition on its own, not just the first.
Step 4: Your entry for this chapter's playbook
Fill this in for yourself — it's not saved or sent anywhere, just yours to keep (copy it into your own notes before you close this tab).