pytestgen-ai
Auto-generate pytest tests for Python functions using Groq AI.
What it does
pytestgen-ai reads your Python files, extracts all public functions using AST parsing, sends each function to Groq AI in parallel, and writes a complete test_<filename>.py file with pytest test cases — including a coverage report.
your code → AST parsing → Groq AI (parallel) → test file → coverage report
Installation
pip install pytestgen-ai
Set your Groq API key (get one free at console.groq.com):
export GROQ_API_KEY=your_key_here
Or create a .env file in your project:
GROQ_API_KEY=your_key_here
Usage
Single file:
pytestgen-ai auth.py
Entire folder:
pytestgen-ai --folder src/
Custom output directory:
pytestgen-ai auth.py --output tests/
pytestgen-ai --folder src/ --output tests/
Example
Given auth.py:
def login(username, password):
if not username or not password:
raise ValueError("Username and password required")
if username == "admin" and password == "secret":
return True
return False
Running:
pytestgen-ai auth.py
Output:
Processing: auth.py
Found 2 function(s): ['login', 'is_strong_password']
Tests written to: test_auth.py
Running coverage...
Name Stmts Miss Cover Missing
----------------------------------------
auth.py 14 0 100%
----------------------------------------
✅ All tests passed.
Generated test_auth.py:
import pytest
from auth import login
def test_normal_case_login():
assert login("admin", "secret") == True
def test_empty_username_login():
with pytest.raises(ValueError):
login("", "secret")
def test_invalid_credentials_login():
assert login("user", "wrong") == False
How it works
- AST parsing — uses Python's built-in
astmodule to extract every public function and class from your file - External call detection — automatically detects DB calls, requests, and other external dependencies that need mocking
- File type detection — detects if the file uses DB, Selenium, or requests
- Groq AI (parallel) — sends all functions to Groq simultaneously using async, so large files generate faster
- Test file — writes a complete
test_<filename>.pyready to run with pytest - Coverage — automatically runs
pytest --covand displays the report
Supported file types
| File type | Support |
|---|---|
| Pure functions | ✅ Full support |
| Input validation | ✅ Full support |
| Class methods | ✅ Supported — class is imported, methods called via instance |
| Database (psycopg2, SQLAlchemy) | ⚠️ Partial — external calls mocked automatically |
| requests / httpx | ⚠️ Partial — external calls mocked automatically |
| Selenium | ❌ Skipped — browser automation not supported |
REST API
pytestgen-ai also ships with a FastAPI endpoint. Install with the optional API dependencies:
pip install pytestgen-ai[api]
Then run:
uvicorn api:app --reload
Upload a .py file via POST /generate and get the generated test content back as JSON.
Interactive docs available at http://127.0.0.1:8000/docs.
Tech stack
- Typer — CLI
- Groq API — AI test generation (llama-3.3-70b-versatile)
- Python AST — function extraction and external call detection
- pytest-cov — coverage reports
- FastAPI — REST API
Limitations
- Works best with pure utility functions and business logic
- Algorithm-heavy code (linked lists, trees, monotonic stacks) may have incorrect expected values in assertions — the AI traces complex pointer logic imperfectly
- Duplicate return values across functions in the same file may cause test confusion
- Generated tests should always be reviewed before committing to production
- Requires a Groq API key (free tier available)
License
MIT
Metadata
Release files for pytestgen-ai 0.1.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| pytestgen_ai-0.1.0.tar.gz | 15.8 kB | Without provenance | Details for pytestgen_ai-0.1.0.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| pytestgen_ai-0.1.0-py3-none-any.whl | Python 3 | none | any | Details for pytestgen_ai-0.1.0-py3-none-any.whl |
Total release size: 29.3 kB
Release files / pytestgen_ai-0.1.0.tar.gz
| Download URL | pytestgen_ai-0.1.0.tar.gz |
|---|---|
| Size | 15.8 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
0182813d8131683ea56a855b020902cb7fea9aec51b92de74ec728501072eabf
|
|
BLAKE2b-256 checksum How to use checksums |
4d78cd1471ef33c266e06020b57f4de4331a31cae5539fe7f9a8aaba1c3b9863
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.2.0 CPython/3.14.2
|
Release files / pytestgen_ai-0.1.0-py3-none-any.whl
| Download URL | pytestgen_ai-0.1.0-py3-none-any.whl |
|---|---|
| Size | 13.4 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
2cf106f7744d8d8982375b5c78702ef11c888c9ea7d9a397e417854356ee3d87
|
|
BLAKE2b-256 checksum How to use checksums |
eab53b78923372e8d3ef17dd4bbde600b0ece665def7c69479cc9a31bbab623f
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.2.0 CPython/3.14.2
|
Security for pytestgen-ai 0.1.0
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.