RUNLOCALAIv38
->Will it run?Best GPUCompareTroubleshootStartLearnPulseModelsHardwareToolsBench
Run check
RUNLOCALAI

Independently operated catalog for local-AI hardware and software. Hand-written verdicts. Source-cited claims. Reproducible commands when we have them.

OP·Fredoline Eruo
DIR
  • Models
  • Hardware
  • Tools
  • Benchmarks
TOOLS
  • Will it run?
  • Compare hardware
  • Cost vs cloud
  • Choose my GPU
  • Prompting kits
  • Quick answers
REF
  • All buyer guides
  • Learn local AI
  • Methodology
  • Glossary
  • Errors KB
  • Trust
EDITOR
  • About
  • Author
  • How we make money
  • Editorial policy
  • Contact
LEGAL
  • Privacy
  • Terms
  • Sitemap
MAIL · MONTHLY DIGEST
Get monthly local AI changes
Monthly recap. No spam.
DISCLOSURE

Some links on this site are affiliate links (Amazon Associates and other first-class retailers). When you buy through them, we earn a small commission at no extra cost to you. Affiliate links do not influence our verdicts — there are cards we rate highly that we don't have affiliate relationships with, and cards that sell well that we refuse to recommend. Read more →

© 2026 runlocalai.coIndependently operated
RUNLOCALAI · v38
  1. >
  2. Home
  3. /Learn
  4. /How-to
  5. /How to generate unit tests for a Python function using Claude Code
HOW-TO · DEV

How to generate unit tests for a Python function using Claude Code

intermediate·15 min·By Fredoline Eruo
Target environment
Ubuntu 24.04 · Ollama 0.4.x
PREREQUISITES

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

  1. Open a terminal and navigate to the project directory containing the Python file with the function to test.

  2. Start Claude Code in interactive mode:

claude
  1. 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%.
  1. Claude Code reads the specified file, understands the function signature and logic, and generates a test file (e.g., tests/test_pricing.py).

  2. 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.

  3. Run the generated test file with pytest to verify all tests pass:

pytest tests/test_pricing.py -v
  1. 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.

  2. 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.

  3. Add the generated test file to the repository with git add tests/test_pricing.py and commit it.

  4. 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's assert statements. Convert the assertions by replacing self.assertEqual(actual, expected) with assert 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 --mock flag or explicitly ask Claude Code to add @pytest.fixture or unittest.mock.patch decorators for the external calls.
  • pytest not in PATH: Running pytest produces a "command not found" error if pytest is not installed or the virtual environment is not activated. Activate the virtual environment with source venv/bin/activate before 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 diff to compare the before and after state.

Related guides

  • How to use AI to write regression test cases for a bug you just fixed
  • How to use an AI assistant to analyze a stack trace and pinpoint the root cause of a bug
← All how-to guidesCourses →