Skip to content
Back to skills

Collection

ASecurity

Scaffolds pytest unit tests for Python backend functions and classes. Use when creating tests for FastAPI endpoints, services, and utilities.

  • 24 stars
  • 0 votes
  • 0 copies
  • 0 views
  • Added September 8, 2026
testingpythongofastapitestingapidatabasebackendfullstack

Works with

  • cli
  • api

Security analysis

A100/100

Pro scans all 21 files and shows the line behind each finding

Scanned September 8, 2026

npx -y skills add mattnigh/skills_collection --skill collection --agent claude-code

Installs into .claude/skills of the current project.

Are you the author of Collection?

Add the live security badge to your README. It updates with every re-scan.

Security grade badge for Collection
[![Security: A — Skills Directory](https://www.skillsdirectory.com/api/skills/mattnigh-collection-cd48f4ca/badge)](https://www.skillsdirectory.com/skills/mattnigh-collection-cd48f4ca)

More formats (shields.io, HTML) on the badges page. Keep it an A: scan every change in CI with Pro.

Download with Pro
SKILL.md
---
name: pytest-test-scaffolder
description: Scaffolds pytest unit tests for Python backend functions and classes. Use when creating tests for FastAPI endpoints, services, and utilities.
---

# Pytest Test Scaffolder

Generates comprehensive unit tests for Python backend code using pytest with Pydantic validation, Firestore mocking, and Genkit integration testing patterns.

## Workflow

1. **Identify function/class to test:**
   - Ask for file path (e.g., `backend/app/api/endpoints/profiles.py`)
   - Read file to extract:
     - Function/class name
     - Parameters and type annotations
     - Return types
     - Dependencies (Firestore, Genkit, services)
   - Determine test location: `backend/app/tests/{{module}}/test_{{name}}.py`

2. **Analyze code structure:**
   - Extract function signature and parameters
   - Identify external dependencies (database, API, AI services)
   - Determine input validation (Pydantic models)
   - Identify error cases (ValueError, ValidationError, HTTPException)
   - Check for async/await patterns

3. **Generate test file:**
   - Replace placeholders in template:
     - `{{FUNCTION_NAME}}` - Function being tested
     - `{{FUNCTION_MODULE}}` - Module import path
     - `{{PARAMETERS}}` - Function parameters with defaults
     - `{{RETURN_TYPE}}` - Expected return type
     - `{{HAPPY_PATH_TESTS}}` - Success scenarios
     - `{{ERROR_TESTS}}` - Exception handling
     - `{{FIXTURES}}` - Mock/fixture setup
   - Write to: `backend/app/tests/{{module}}/test_{{name}}.py`

4. **Include comprehensive test scenarios:**
   - ✅ **Happy Path**: Valid inputs produce expected output
   - ✅ **Validation Error**: Invalid Pydantic models rejected (422)
   - ✅ **Not Found Error**: Resource not found handled (404)
   - ✅ **Permission Error**: Unauthorized access rejected (401)
   - ✅ **Server Error**: Exceptions caught and logged (500)
   - ✅ **Mock Dependencies**: Firestore, Genkit, services mocked
   - ✅ **Async Support**: Use pytest-asyncio for async functions
   - ✅ **Fixture Reuse**: Shared fixtures in conftest.py

5. **Use pytest best practices:**
   - Use fixtures for mock setup (Firestore, Firebase, Genkit)
   - Use `monkeypatch` for environment variables
   - Use `@pytest.mark` for test categorization
   - Mock external services (Firestore, Genkit flows)
   - Test both sync and async functions
   - Use descriptive test names: `test_{{function}}_{{scenario}}`

6. **Report success:**
   - Show test file path
   - Display test count
   - Provide run command: `pytest backend/app/tests/{{module}}/test_{{name}}.py -v`
   - Link to mock patterns in references/

## Test Categories

### Endpoint Tests

```python
# Happy path: Valid request → 200 response
def test_create_user_success(client, monkeypatch):
    mock_firestore = monkeypatch.setattr(...)
    response = client.post("/api/users", json={...})
    assert response.status_code == 200
    assert response.json()["id"]

# Validation error: Invalid input → 422 response
def test_create_user_validation_error(client):
    response = client.post("/api/users", json={"name": ""})
    assert response.status_code == 422

# Permission error: No auth → 401 response
def test_create_user_unauthorized(client):
    response = client.post("/api/users", json={...})
    assert response.status_code == 401

# Not found: Resource missing → 404 response
def test_get_user_not_found(client):
    response = client.get("/api/users/invalid-id")
    assert response.status_code == 404
```

### Service Tests

```python
# Mock Firestore for service tests
@pytest.fixture
def mock_firestore(monkeypatch):
    mock_db = MagicMock()
    monkeypatch.setattr("app.services.firestore_client", mock_db)
    return mock_db

def test_service_with_firestore(mock_firestore):
    mock_firestore.collection.return_value.document.return_value.get.return_value.exists = True
    result = UserService.get_user("user-123")
    assert result is not None
```

### Genkit Flow Tests

```python
# Mock Genkit responses
@pytest.fixture
def mock_genkit(monkeypatch):
    mock_flow = MagicMock()
    monkeypatch.setattr("app.ai.genkit_service.flow", mock_flow)
    return mock_flow

@pytest.mark.ai_services
async def test_genkit_flow_execution(mock_genkit):
    mock_genkit.return_value = {"text": "Generated response"}
    result = await ai_service.generate_content(prompt="test")
    assert "Generated" in result
```

### Async Function Tests

```python
@pytest.mark.asyncio
async def test_async_database_query(client, monkeypatch):
    mock_db = AsyncMock()
    mock_db.get_user.return_value = {"id": "123", "name": "Test"}
    monkeypatch.setattr("app.db.get_user", mock_db)

    result = await fetch_user_profile("123")
    assert result["name"] == "Test"
```

## Fixtures & Mocking

See `references/backend-test-patterns.md` for:

- Firestore mocking patterns
- Firebase Auth mocking
- Genkit flow mocking
- TestClient setup for endpoints
- Async fixture setup
- Pydantic model factories

## Template Files

Templates located in `.claude/skills/pytest-test-scaffolder/templates/`:

- `endpoint.test.py.tpl` - FastAPI endpoint tests
- `service.test.py.tpl` - Service/utility tests
- `async.test.py.tpl` - Async function tests
- `conftest.py.tpl` - Shared fixture template

## Integration with Testing Strategy

- **testing-specialist**: Uses this skill to generate backend tests
- **test-automation-specialist**: Parallelizes backend test generation across modules
- **test-runner**: Executes generated tests via `pytest backend/app/tests/ -v`
- **fullstack-integration-specialist**: Uses alongside api-integration-test-scaffolder

## Coverage Goals

- **Current**: 85% (manual testing)
- **Target**: 95% with scaffolded tests
- **Priority**: Critical path endpoints → service layer → utilities

Run `pytest backend/app/tests/ --cov=app --cov-report=html` to measure coverage.

Files in this skill

  • 0Chan-smc__claude-code-workflow-lab__claude__skills__frontend-dev-guidelines__SKILL.md15.1 KB
  • 17hz__nextjs-template__claude__skills__example-skill__SKILL.md316 B
  • 1ambda__dataops-platform__claude__skills__context-synthesis__SKILL.md3.5 KB
  • 1natsu172__dotfiles__claude__skills__git-analysis__SKILL.md5.4 KB
  • 1natsu172__dotfiles__claude__skills__github-pr-best-practices__SKILL.md7.7 KB
  • 23Maestro__prospect-pipeline__claude__skills__npid-fastapi-skill.md26.1 KB
  • 360AYA25__ClaudeN8N__claude__skills__n8n-code-javascript__SKILL.md15.7 KB
  • 360AYA25__ClaudeN8N__claude__skills__n8n-code-python__SKILL.md17.5 KB
  • 360AYA25__ClaudeN8N__claude__skills__n8n-expression-syntax__SKILL.md9.4 KB
  • 360AYA25__ClaudeN8N__claude__skills__n8n-mcp-tools-expert__SKILL.md12.5 KB
  • 360AYA25__ClaudeN8N__claude__skills__n8n-node-configuration__SKILL.md16.6 KB
  • 360AYA25__ClaudeN8N__claude__skills__n8n-workflow-patterns__SKILL.md11.2 KB
  • 3x-Projetos__claude-memory-framework__claude__skills__scientist__SKILL.md14.8 KB
  • 5MinFutures__futures-arena__claude__skills__migration-tracker__SKILL.md16.2 KB
  • 5MinFutures__futures-arena__claude__skills__planning-guidelines__SKILL.md11.8 KB
  • 92Bilal26__TaskPilotAI__claude__skills__assessment-builder__SKILL.md17.5 KB
  • 92Bilal26__TaskPilotAI__claude__skills__book-scaffolding__SKILL.md19.1 KB
  • 92Bilal26__TaskPilotAI__claude__skills__code-validation-sandbox__SKILL.md6.2 KB
  • 92Bilal26__TaskPilotAI__claude__skills__exercise-designer__SKILL.md18.1 KB
  • 92Bilal26__TaskPilotAI__claude__skills__learning-objectives__SKILL.md24.5 KB

Attribution

Is this your skill, or is something wrong with this listing? Request removal or report an issue. Author removals are honored within 72 hours.

Comments

Loading comments…