If an AI agent writes both your tests and your code, you’re just verifying that the model agrees with itself. Real Python’s latest tutorial tackles this circular logic by enforcing a strict separation of duties: humans design the tests, and AI agents write the implementation. Published on November 17, 2026, the guide outlines a five-step workflow that keeps developers in control while leveraging the drafting speed of tools like Claude Code, GitHub Copilot CLI, and OpenCode.
The Five-Step Control Loop
The core of this methodology is a cyclical process where the developer handles design and review, while the agent handles drafting and coding. The loop begins with the human defining a contract and writing the first failing test by hand. This initial test acts as the anchor, ensuring the agent has a clear, unambiguous target. The agent then drafts additional tests based on that contract, but crucially, it does so before any implementation code exists. This prevents the common pitfall where tests are reverse-engineered from buggy or incomplete code.
Fencing the Tests
A key technical takeaway from the tutorial is the concept of 'fencing' the test directory. Before the agent writes the implementation code, developers must configure their coding agent’s permissions to deny writes to the tests/ folder. In the example using Claude Code, this involves creating a .claude/settings.json file with a rule like Edit(/tests/**). This hard constraint ensures the agent cannot cheat by modifying the test expectations to match a flawed implementation. If the agent tries to edit a test to make it pass, the permission system blocks the action, forcing the agent to fix the code instead.
Hardening the Contract
The tutorial emphasizes that agents often make silent design decisions. When an agent drafts tests, it might add edge cases—like sorting lists or handling trailing zeros—that weren’t explicitly in your initial contract. The developer’s job in Step 3 is to 'harden' the tests by reviewing these additions. You must decide whether to keep these new behaviors as promises or delete them. The article warns that silently accepting drafted tests from under-specced contracts can shift the implementation away from your original intent, creating technical debt before the first line of production code is even written.
Key Takeaways
- Separation of Concerns: Humans define the contract and initial tests; agents draft additional tests and write implementation code.
- Permission Fencing: Use agent-specific permission settings to block writes to the
tests/directory during the implementation phase. - Contract Review: Actively review agent-drafted tests to accept or reject implicit design decisions they introduce.
- Red-Green-Refactor: The traditional TDD cycle remains, but the 'green' phase is now delegated to the AI agent under strict constraints.
The Bottom Line
Letting AI agents write tests is a trap that creates circular validation; keep humans in charge of defining the contract and use permission fences to force agents to solve the problem, not change the rules.