How to generate unit tests for a Python function using Claude Code
Claude Code CLI installed and authenticated, Python function to test, pytest installed in the project environment
What this does
Claude Code is a command-line interface for interacting with Claude models to automate development tasks. When given a Python function, Claude Code can analyze its behavior, identify edge cases, and generate a complete pytest-compatible test file. The generated tests follow pytest conventions and include assertions for normal inputs, boundary values, error conditions, and any custom validation logic present in the function.
Steps
Open a terminal and navigate to the project directory containing the Python file with the function to test.
Start Claude Code in interactive mode:
claude
- Once inside the Claude Code session, enter the following prompt to generate tests for a specific function:
Generate a pytest test file for the `calculate_discount` function in src/pricing.py.
Create tests for the following scenarios: normal discount application, zero price,
negative discount rate, discount exceeding 100%, and boundary at exactly 100%.
Claude Code reads the specified file, understands the function signature and logic, and generates a test file (e.g.,
tests/test_pricing.py).Review the generated test file in the editor or by using the Claude Code read command. Confirm the test names are descriptive and the assertions match the expected behavior.
Run the generated test file with pytest to verify all tests pass:
pytest tests/test_pricing.py -v
Inspect the output. Each test should appear as a separate row with PASSED status. If any test fails, the output shows the expected vs. actual values.
If a test fails because the function behavior differs from the documentation, ask Claude Code to adjust the test to match the actual behavior, or file a separate issue to correct the function documentation.
Add the generated test file to the repository with
git add tests/test_pricing.pyand commit it.Run the entire test suite to ensure the new tests do not conflict with existing tests:
pytest tests/ -v
Verification
Running pytest tests/test_pricing.py -v should list all generated test functions with a PASSED status. The exit code should be 0, and the output should show a summary such as "5 passed in 0.42s."
Common failures
- Function not found in the specified file: Claude Code searches the file for the function name. If the function uses a different name, imports are missing, or the file path is incorrect, the generation fails. Provide the exact file path and function name in the prompt.
- Generated tests use incorrect assertion syntax: Claude Code sometimes generates assertions using
assertEqual(unittest style) instead of pytest'sassertstatements. Convert the assertions by replacingself.assertEqual(actual, expected)withassert actual == expected. - Mock dependencies not accounted for: If the function calls external services or database connections, the generated test may not mock those dependencies. Use the
--mockflag or explicitly ask Claude Code to add@pytest.fixtureorunittest.mock.patchdecorators for the external calls. - pytest not in PATH: Running
pytestproduces a "command not found" error if pytest is not installed or the virtual environment is not activated. Activate the virtual environment withsource venv/bin/activatebefore running pytest. - Generated test file overwrites existing tests: If a file already exists at the target path, Claude Code may append or replace its contents. Review the file after generation to confirm existing tests are preserved. Use
git diffto compare the before and after state.