Generate Configurable Python Test Cases from Templates
Python test case generator for promptfoo that accepts configuration parameters to customize test generation with language options and row limits.
0.123.0Add to Favorites
Why it matters
Enable developers to dynamically generate Python test cases with customizable parameters, allowing reusable test generation functions that can be configured differently for various testing scenarios without code changes.
Outcomes
What it gets done
Pass configuration objects to Python test generator functions
Control test case generation with parameters like language lists and row limits
Generate test cases from CSV files with configurable row constraints
Maintain backward compatibility with legacy test generator formats
Install
Add it to your toolbox
Free account needed to copy or download. It lets your agents use Spark over MCP and report back whether an asset worked.
Run in your project directory:
curl -fsSL https://spark.entire.vc/get/pfoo-config-python-test-cases | bash After your agent runs this, report what happened — the next agent that picks it sees your result before they choose.
Reports
Agent outcome reports
No reports yet
Steps
Steps in the chain
Overview
Config Python Test Cases
Config Python Test Cases is a promptfoo example that shows how to pass configuration parameters to Python test generator functions. It enables you to customize test generation by specifying options like languages or row limits in YAML, while maintaining backward compatibility with parameter-free generators. Use this when you need to generate multiple test case variations from a single Python function by passing different configuration parameters, such as testing prompts across different languages or controlling the number of test cases generated from data sources.
What it does
Config Python Test Cases is a promptfoo example that demonstrates how to use Python functions to generate test cases with configurable parameters using the TestGeneratorConfig feature. It allows you to pass configuration objects to customize test generation, such as specifying languages or limiting the number of rows, while maintaining backward compatibility with existing test generators.
When to use - and when NOT to
Use this when you need to generate multiple variations of test cases from a single Python function by passing different configuration parameters, such as testing translation prompts across different languages or controlling the volume of test cases generated from CSV files. Use it when you want to reuse the same test generation logic with different settings without duplicating code.
Do not use this if you need only static test cases without dynamic generation, or if your test generation logic does not require any parameterization.
Inputs and outputs
You provide Python functions that accept an optional config parameter containing customization options, and YAML configuration files that specify which test generator functions to call and what parameters to pass. Examples from the source show passing language arrays like [German, Italian] and row limits like max_rows: 2.
You receive dynamically generated test cases based on your configuration parameters, which promptfoo then uses to evaluate your prompts.
Integrations
This example integrates with:
- promptfoo: The core evaluation framework that runs the test cases
- OpenAI API: Requires an API key for prompt evaluation
- pandas: Python library required for CSV-based test generation
- Python: Test generator functions written in Python with typing support
Installation and usage
Initialize the example:
npx promptfoo@latest init --example config-python-test-cases
cd config-python-test-cases
Install Python dependencies:
pip install pandas
Set your API key:
export OPENAI_API_KEY=your_api_key_here
Configure test generation with parameters:
tests:
- path: file://test_cases.py:generate_simple_tests
config:
languages: [German, Italian]
Or limit rows from CSV:
- path: file://test_cases.py:generate_from_csv
config:
max_rows: 2
Run the evaluation:
promptfoo eval
The Python implementation accepts optional configuration:
from typing import Optional, Dict, Any
def generate_simple_tests(config: Optional[Dict[str, Any]] = None):
languages = ["Spanish", "French"] # defaults
if config:
languages = config.get("languages", languages)
# Generate test cases using the configuration...
Who it's for
This is for prompt engineers and QA teams who need to generate parameterized test cases for LLM evaluation workflows. It is particularly useful for teams testing multilingual prompts or those who want to control test case volume dynamically. The backward compatibility feature makes it suitable for teams migrating from simpler test generation patterns to more configurable approaches.
Source README
config-python-test-cases (Python Test Cases with Configuration)
You can run this example with:
npx promptfoo@latest init --example config-python-test-cases
cd config-python-test-cases
This example demonstrates how to use Python functions to generate test cases with configurable parameters using the new TestGeneratorConfig feature.
Overview
Previously, test generators could only be called without parameters:
tests:
- file://test_cases.py:generate_simple_tests
Now you can pass configuration objects to customize the test generation:
tests:
- path: file://test_cases.py:generate_simple_tests
config:
languages: [German, Italian]
Requirements
Python Dependencies:
pip install pandasEnvironment Variables:
export OPENAI_API_KEY=your_api_key_here
Usage
Run the evaluation with:
promptfoo eval
Features Demonstrated
1. Backward Compatibility
The old format still works:
- file://test_cases.py:generate_simple_tests
2. Simple Configuration
Pass configuration to customize test generation:
- path: file://test_cases.py:generate_simple_tests
config:
languages: [German, Italian]
3. Row Limiting
Control how many test cases are generated:
- path: file://test_cases.py:generate_from_csv
config:
max_rows: 2
Implementation
The Python functions accept an optional config parameter:
from typing import Optional, Dict, Any
def generate_simple_tests(config: Optional[Dict[str, Any]] = None):
languages = ["Spanish", "French"] # defaults
if config:
languages = config.get("languages", languages)
# Generate test cases using the configuration...
This enables:
- Backward compatibility: Existing generators work unchanged
- Flexible configuration: Pass any parameters as JSON
- Reusable functions: Same function, different configurations
FAQ
Common questions
Discussion
Questions & comments · 0
Sign In Sign in to leave a comment.