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 GitHubvanman2024/cli-builder
cli-builder
plugins/cli-builder/skills/cli-testing-patterns/SKILL.md
January 22, 2026
Select agents to install to:
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-patternsInstallation paths:
.claude/skills/cli-testing-patterns/# 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