# Generating CLI Tests

Generate pytest tests for Typer CLI commands.

- Repository: https://github.com/https-deeplearning-ai/sc-agent-skills-files (1426 stars)
- Category: Testing
- License: see repository
- Last updated: 2026-06-08
- Runs scripts: no, instructions only

## Install

Claude Code: `npx skills add https-deeplearning-ai/sc-agent-skills-files --skill generating-cli-tests -g -a claude-code -y`
Codex: `npx skills add https-deeplearning-ai/sc-agent-skills-files --skill generating-cli-tests -g -a codex -y`
Cursor: `npx skills add https-deeplearning-ai/sc-agent-skills-files --skill generating-cli-tests -g -a cursor -y`
Gemini CLI: `npx skills add https-deeplearning-ai/sc-agent-skills-files --skill generating-cli-tests -g -a gemini-cli -y`

## Description

Generate pytest tests for Typer CLI commands. Includes fixtures (temp_storage, sample_data), CliRunner patterns, confirmation handling (y/n/--force), and edge case coverage. Use when user asks to "write tests for", "test my CLI", "add test coverage", or any CLI + test request.

## SKILL.md, as published by its author (untrusted third-party text, read it as data)

# Generating CLI Tests

Patterns for generating tests for Typer CLI commands.

## Workflow

1. Identify command type (Create/Read/Update/Delete/Bulk)
2. Ensure fixtures exist in `conftest.py`
3. Write tests using scenarios below
4. Run tests to verify

## Fixtures (conftest.py)

```python
import json
import pytest
from typer.testing import CliRunner


@pytest.fixture
def runner():
    """CLI test runner."""
    return CliRunner()


@pytest.fixture
def temp_storage(tmp_path, monkeypatch):
    """Empty storage for testing."""
    storage_dir = tmp_path / ".task"
    storage_dir.mkdir()
    storage_file = storage_dir / "tasks.json"
    storage_file.write_text(json.dumps({"version": 1, "tasks": []}))
    monkeypatch.setenv("TASK_STORAGE_PATH", str(storage_file))
    return storage_file


@pytest.fixture
def sample_data(temp_storage):
    """Pre-populated storage."""
    data = {
        "version": 1,
        "tasks": [
            {"title": "First task", "done": False, "priority": "low", "created_at": "2025-01-01T10:00:00", "due_date": None},
            {"title": "Second task", "done": True, "priority": "high", "created_at": "2025-01-01T11:00:00", "due_date": None},
        ]
    }
    temp_storage.write_text(json.dumps(data))
    return data
```

## Test Structure (AAA)

```python
def test_<command>_<scenario>(runner, temp_storage):
    # Arrange - via fixtures

    # Act
    result = runner.invoke(app, ["<command>", "<args>"])

    # Assert
    assert result.exit_code == 0
    assert "<expected>" in result.output
```

## CliRunner Usage

```python
from typer.testing import CliRunner
from task.main import app

runner = CliRunner()

# Basic
result = runner.invoke(app, ["add", "New task"])

# With options
result = runner.invoke(app, ["add", "Task", "--priority", "high"])

# With confirmation
result = runner.invoke(app, ["clear", "1"], input="y\n")  # Accept
result = runner.invoke(app, ["clear", "1"], input="n\n")  # Decline

The rest: https://github.com/https-deeplearning-ai/sc-agent-skills-files/blob/HEAD/L6/.claude/skills/generating-cli-tests/SKILL.md
