Back to Skills

cli-testing-patterns

verified

CLI testing strategies and patterns for Node.js (Jest) and Python (pytest, Click.testing.CliRunner). Use when writing tests for CLI tools, testing command execution, validating exit codes, testing output, implementing CLI test suites, or when user mentions CLI testing, Jest CLI tests, pytest CLI, Click.testing.CliRunner, command testing, or exit code validation.

View on GitHub

Marketplace

cli-builder

vanman2024/cli-builder

Plugin

cli-builder

development

Repository

vanman2024/cli-builder

plugins/cli-builder/skills/cli-testing-patterns/SKILL.md

Last Verified

January 22, 2026

Install Skill

Select agents to install to:

Scope:
npx add-skill https://github.com/vanman2024/cli-builder/blob/main/plugins/cli-builder/skills/cli-testing-patterns/SKILL.md -a claude-code --skill cli-testing-patterns

Installation paths:

Claude
.claude/skills/cli-testing-patterns/
Powered by add-skill CLI

Instructions

# CLI Testing Patterns

Comprehensive testing strategies for CLI applications using industry-standard testing frameworks. Covers command execution testing, exit code validation, output verification, interactive prompt testing, and integration testing patterns.

## Instructions

### When Testing Node.js CLI Tools

1. **Use Jest for testing CLI commands**
   - Import `child_process.execSync` for command execution
   - Create helper function to run CLI and capture output
   - Test exit codes, stdout, stderr separately
   - Handle both success and error cases

2. **Test Structure**
   - Set up CLI path relative to test location
   - Create `runCLI()` helper that returns `{stdout, stderr, code}`
   - Use try-catch to handle non-zero exit codes
   - Test common scenarios: version, help, unknown commands

3. **What to Test**
   - Command execution with various argument combinations
   - Exit code validation (0 for success, non-zero for errors)
   - Output content (stdout) validation
   - Error messages (stderr) validation
   - Configuration file handling
   - Interactive prompts (with mocked input)

### When Testing Python CLI Tools

1. **Use pytest with Click.testing.CliRunner**
   - Import `CliRunner` from `click.testing`
   - Create runner fixture for reusable test setup
   - Invoke commands with `runner.invoke(cli, ['args'])`
   - Check `result.exit_code` and `result.output`

2. **Test Structure**
   - Create pytest fixture for CliRunner instance
   - Use `runner.invoke()` to execute CLI commands
   - Access results through `result` object
   - Simulate interactive input with `input='responses\n'`

3. **What to Test**
   - Command invocation with various arguments
   - Exit code validation
   - Output content verification
   - Error handling and messages
   - Interactive prompt responses
   - Configuration handling

### Exit Code Testing Patterns

**Standard Exit Codes:**
- `0` - Success
- `1` - General error
- `2` - Misuse of command (invalid arguments)
- `126` - Comm

Validation Details

Front Matter
Required Fields
Valid Name Format
Valid Description
Has Sections
Allowed Tools
Instruction Length:
5023 chars