Write Unit Tests for a Small Script with Aider
Learn testing by having an open-source coding agent draft tests for your script, then reading every one to understand what it checks.
Why this works
Testing is easiest to learn from examples. Aider can draft tests for a small Python script; your job is to read them, run them, and make them fail on purpose once to prove they work.
Step by step
- Start with a tiny scriptPick a script under 100 lines, such as a unit converter or a budget calculator, and commit it.
- Ask for testsUse the prompt below in Aider with the script added to the chat.
- Run the testsRun
pytest(install it first). Read the output even when everything passes. - Break the code on purposeChange one line of the script to produce a wrong answer and confirm a test fails.
- Explain each test to yourselfFor each test, write one sentence saying what behaviour it protects.
Copy-paste prompt
Write pytest unit tests for the functions in this file. Cover normal inputs, edge cases such as zero and negative values, and one error case. Do not change the script. Keep each test short with a descriptive name. SCRIPT: <paste>
Check your result
- Tests run and pass on the correct code
- At least one test fails when you break the code on purpose
- You can explain each test
Pitfalls to avoid
- A passing test is not proof of correct behaviour if the test asserts the wrong thing.
- Do not accept generated tests that only call the function without checking the result.
What we checked: Licence and project status were read from the project's GitHub repository on 8 Oct 2026. Install commands and flags change between releases, so follow the project README for the current version. Source: https://github.com/Aider-AI/aider.