Skip to main content

Auto-generate pytest tests for Python functions using Groq AI

Project description

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

  1. AST parsing — uses Python's built-in ast module to extract every public function and class from your file
  2. External call detection — automatically detects DB calls, requests, and other external dependencies that need mocking
  3. File type detection — detects if the file uses DB, Selenium, or requests
  4. Groq AI (parallel) — sends all functions to Groq simultaneously using async, so large files generate faster
  5. Test file — writes a complete test_<filename>.py ready to run with pytest
  6. Coverage — automatically runs pytest --cov and 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

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

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pytestgen_ai-0.1.0.tar.gz (15.8 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

pytestgen_ai-0.1.0-py3-none-any.whl (13.4 kB view details)

Uploaded Python 3

File details

Details for the file pytestgen_ai-0.1.0.tar.gz.

File metadata

  • Download URL: pytestgen_ai-0.1.0.tar.gz
  • Upload date:
  • Size: 15.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.14.2

File hashes

Hashes for pytestgen_ai-0.1.0.tar.gz
Algorithm Hash digest
SHA256 0182813d8131683ea56a855b020902cb7fea9aec51b92de74ec728501072eabf
MD5 b24dac768d5ecfca9a1eed6f178eabe5
BLAKE2b-256 4d78cd1471ef33c266e06020b57f4de4331a31cae5539fe7f9a8aaba1c3b9863

See more details on using hashes here.

File details

Details for the file pytestgen_ai-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: pytestgen_ai-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 13.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.14.2

File hashes

Hashes for pytestgen_ai-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 2cf106f7744d8d8982375b5c78702ef11c888c9ea7d9a397e417854356ee3d87
MD5 466d03fb6ecf7371aba9ee34627adac8
BLAKE2b-256 eab53b78923372e8d3ef17dd4bbde600b0ece665def7c69479cc9a31bbab623f

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page