home / skills / fortiumpartners / ai-mesh / pytest-test

pytest-test skill

/skills/pytest-test

This skill executes and generates pytest tests for Python projects, including fixtures, parametrization, and mocking support to boost test quality.

npx playbooks add skill fortiumpartners/ai-mesh --skill pytest-test

Review the files below or copy the command above to add this skill to your agents.

Files (3)
SKILL.md
1.4 KB
---
name: pytest Test Framework
description: Execute and generate pytest tests for Python projects with fixtures, parametrization, and mocking support
version: 1.0.0
---

# pytest Test Framework

## Purpose

Provide pytest test execution and generation for Python projects, supporting:
- Test file generation from templates
- Test execution with structured output
- Fixtures and parametrized tests
- Mock and monkeypatch support

## Usage

### Generate Test File

```bash
python generate-test.py \
  --source src/calculator.py \
  --output tests/test_calculator.py \
  --type unit \
  --description "Calculator fails to handle division by zero"
```

### Execute Tests

```bash
python run-test.py \
  --file tests/test_calculator.py \
  --config pytest.ini
```

## Output Format

### Test Generation
```json
{
  "success": true,
  "testFile": "tests/test_calculator.py",
  "testCount": 3,
  "template": "unit-test"
}
```

### Test Execution
```json
{
  "success": false,
  "passed": 2,
  "failed": 1,
  "total": 3,
  "duration": 0.234,
  "failures": [
    {
      "test": "test_divide_by_zero",
      "error": "AssertionError: Expected ZeroDivisionError",
      "file": "tests/test_calculator.py",
      "line": 15
    }
  ]
}
```

## Integration

Used by deep-debugger for Python project testing:
1. Invoke test-detector to identify pytest
2. Invoke generate-test.py to create failing test
3. Invoke run-test.py to validate test fails
4. Re-run after fix to verify passing

Overview

This skill executes and generates pytest tests for Python projects, focusing on fixtures, parametrization, and mocking support. It produces test files from templates and runs tests with structured JSON output to drive automation and CI feedback. The skill is designed to create failing tests for debugging workflows and to verify fixes by re-running test suites.

How this skill works

The skill inspects a Python source file or project to generate pytest-compatible test files using configurable templates (unit, integration, etc.). It supports fixtures, parametrized cases, and mock/monkeypatch patterns when producing tests. Test execution runs pytest via a runner script and returns a structured JSON summary with pass/fail counts, duration, and detailed failure entries to aid automated analysis.

When to use it

  • Generate unit tests quickly from a Python source file to capture edge cases such as exceptions
  • Automate test creation and execution as part of a debug workflow that needs reproducible failing tests
  • Run targeted test files in CI with machine-readable results for reporting or gating
  • Verify fixes by regenerating and re-running tests after code changes
  • Create parametrized test suites to validate multiple input combinations efficiently

Best practices

  • Use descriptive test descriptions when generating tests so failures are easy to interpret
  • Prefer fixtures for shared setup and parametrization for variant inputs to keep tests concise
  • Include clear assertions and exception expectations (e.g., pytest.raises) for edge cases
  • Keep generated tests small and focused—one behavior per test—so failures pinpoint the issue
  • Integrate the runner output into CI or a debugger pipeline using the JSON summary for automation

Example use cases

  • Generate a failing unit test that reproduces a division-by-zero bug and run it to confirm failure
  • Create parametrized tests for a calculation function to cover multiple input ranges in one file
  • Mock external dependencies in generated tests to isolate logic under test
  • Run a specific test file in a CI job and parse the JSON output to publish test metrics
  • Drive an automated repair loop: generate failing test, fix code, re-run to verify passing

FAQ

What output format does test execution produce?

Test execution returns a JSON summary with success flag, passed/failed counts, total, duration, and detailed failure entries including file and line.

Can generated tests use fixtures and mocks?

Yes. The generator supports fixtures, parametrization, and mock/monkeypatch patterns to create realistic and isolated tests.