This guide takes you from installation to a verified, generated test suite for your own Python project.
Code2Test needs Python 3.12 or later.
pip install code2test
code2test --versionIf the package index is not available to you, install the release from GitHub: pip install git+https://github.com/Cirkitly/code2test@v0.1.0.
Set an API key and, optionally, the model. Any pydantic-ai model string works:
export ANTHROPIC_API_KEY=...
export CODE2TEST_MODEL=anthropic:claude-sonnet-5-5For an OpenAI-compatible provider, set OPENAI_BASE_URL and use the openai-chat: prefix:
export OPENAI_API_KEY=...
export OPENAI_BASE_URL=https://api.minimax.io/v1
export CODE2TEST_MODEL=openai-chat:MiniMax-M3.1-Flash-PreviewTo check the installation without a key, use CODE2TEST_MODEL=test. It runs the pipeline with a placeholder model, and the tests it produces are discarded.
From the root of the project you want to test:
cd /path/to/your/project
code2test test --auto .Code2Test infers an intent for each function and class, generates tests, runs them and repairs failures. It ends with a summary like this:
Components 13
Components with passing tests 13 (100%)
Tests generated 205
Passed on first run 196 (96%)
Tests kept 205
of which xfail (possible bugs) 0
Tests dropped 0
Repair rounds 8
The tests are in tests/, one file per component. The same numbers are saved to .code2test/metrics.json.
To review intents and tests before they are saved, run code2test test . without --auto. For each component you can accept the intent, edit it, or skip the component; then you accept or skip its tests.
To test only part of a project, pass a file or directory, for example code2test test --auto src/mypkg/utils.py. Paths still resolve to the project root.
code2test verify tests/This runs the suite and exits with status 1 if any test fails. After you change the code, you can repair the generated tests:
code2test verify --diagnose tests/ # explain each failure
code2test verify --fix tests/ # repair generated tests that you have not editedcode2test intent show # all intents
code2test intent show my_func # one function or class
code2test intent edit my_func # correct it; later runs reuse your versioncode2test benchmark .This injects small bugs into the tested code one at a time and reports how many the generated tests catch. It also reports how often the diagnosis agent identifies the cause of a failure correctly. Your source files are restored after every injected bug.
To see the results per component, next to the behaviors each component's tests check:
code2test report coverage # table in the terminal
code2test report coverage --format html # tests/reports/coverage.htmlcode2test test --auto --exit-code .
code2test verify tests/See examples/ci for GitHub Actions and GitLab CI configurations, and architecture.md for how the pipeline works.