Skip to content

Abdullahali77/AI_Testing_CLI

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

14 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Unit Test Generator CLI

A specialized command-line tool that generates Python unit tests for your functions using the Groq LLM API. Built with engineering rigor: AST-based validation, prompt injection prevention, and deterministic output.


Prerequisites


Installation

# 1. Clone the repository
git clone https://github.com/Abdullahali77/AI_Testing_CLI.git
cd AI_Testing_CLI

# 2. Create and activate a virtual environment
python -m venv venv
source venv/bin/activate      # macOS/Linux
venv\Scripts\activate         # Windows

# 3. Install dependencies
pip install -r requirements.txt

# 4. Set up your API key
touch .env # Linux
ni .env -ItemType File # windows powershell
# create .env and put real Groq API key
GROQ_API_KEY= ...

Usage

Generate tests from a file (print to stdout)

python main.py --file path/to/your_module.py

Generate tests and save to a file

python main.py --file path/to/your_module.py --output tests/test_module.py

Pipe source code via stdin

cat your_module.py | python main.py

Use a different Groq model

python main.py --file your_module.py --model mixtral-8x7b-32768

Example

Given example_input.py:

def divide(a: float, b: float) -> float:
    if b == 0:
        raise ValueError("Cannot divide by zero")
    return a / b

Run:

python main.py --file example_input.py

Expected output (printed to stdout):

import unittest
from example_input import divide

class TestDivide(unittest.TestCase):
    def test_divide_positive_numbers(self):
        self.assertAlmostEqual(divide(10, 2), 5.0)

    def test_divide_by_zero_raises_value_error(self):
        with self.assertRaises(ValueError):
            divide(5, 0)
    ...

Running the Tests

pytest tests/ -v

Architecture & Design Choices

AST-Based Validation (core/parser.py)

Instead of fragile regex, the tool uses Python's built-in ast module to parse source code into an Abstract Syntax Tree. This makes function detection mathematically correct — immune to whitespace tricks or unusual formatting.

Comment Stripping for Prompt Injection Prevention (core/sanitizer.py)

A common attack vector is embedding malicious LLM instructions inside comments (e.g., # Ignore previous instructions and...). The tokenize module surgically removes all comments before the source code ever reaches the API.

Deterministic Output (core/llm_client.py)

The API is called with temperature=0, ensuring the model produces consistent, reproducible output rather than creative variations.

Strict System Prompt

The LLM is given a tightly scoped system prompt that forbids it from producing anything other than raw Python test code — no markdown fences, no explanations, no prose.

Out-of-Scope Rejection (utils/validators.py)

Any prompt containing keywords like "explain", "refactor", "debug", or "fix" is rejected immediately before touching the API, returning: Error: This tool only generates unit tests for functions.

Output Post-Processing (utils/formatter.py)

The LLM's response is stripped of markdown fences and validated to confirm it contains test functions before being returned to the user.


Project Structure

AI_Testing_CLI/
├── .gitignore
├── README.md
├── requirements.txt
├── main.py                   # CLI entry point (Typer)
├── core/
│   ├── parser.py             # AST-based function detection
│   ├── sanitizer.py          # Comment stripping (prompt injection prevention)
│   └── llm_client.py         # Groq API integration
├── utils/
│   ├── validators.py         # Out-of-scope request rejection
│   └── formatter.py          # LLM output post-processing
└── tests/
    ├── test_parser.py
    ├── test_sanitizer.py
    ├── test_validators.py
    └── test_formatter.py

About

A specialized command-line tool that generates Python unit tests for your functions using the Groq LLM API. Built with engineering rigor: AST-based validation, prompt injection prevention, and deterministic output.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages