Python Click CliRunner Test Suite Prompt
Design a pytest suite that exercises a Click-based CLI end to end using CliRunner, covering exit codes, stdout/stderr separation, prompts, isolated filesystems, and env-var precedence without touching real systems.
- Target user
- Engineers building Python CLIs who need confidence before release
- Difficulty
- Intermediate
- Tools
- Claude, ChatGPT
The prompt
You are a Python testing specialist who knows a CLI is only as trustworthy as the test that invokes it like a real user would. I will provide: - The Click command group and its commands/options - Which commands have side effects (filesystem, network, subprocess) and how they're isolated - The behaviors I most need to guarantee (exit codes, output format, prompts) Your job: 1. **Set up the runner** — show a `CliRunner` fixture and explain `invoke()`, `mix_stderr=False`, and why asserting on `result.exit_code` plus `result.output` beats parsing logs. 2. **Cover the contract** — write tests for the happy path, each error path (bad flag, missing arg, validation failure), and the `--help` text so the interface itself is regression-protected. 3. **Isolate side effects** — use `runner.isolated_filesystem()` for file work and inject fakes/mocks for network and subprocess calls so tests are hermetic and fast. 4. **Test prompts and env precedence** — drive interactive prompts via `input=`, and assert that env vars, flags, and config resolve in the documented order. 5. **Pin exit-code semantics** — assert specific non-zero codes (e.g. 2 for usage errors) so callers and CI gates depend on stable contracts. 6. **Report coverage gaps** — list any command path or option combination the suite does not yet exercise. Output as: the runnable pytest file, a fixtures section, and a coverage-gap list. Do not let a test hit a real network, real credentials, or the real filesystem outside the isolated dir — flag any command that resists isolation.
Run this prompt with AI
Test it, get an AI-improved version, or compare models — live in the Prompt Workspace. No copy-paste.
Related prompts
-
Add Pytest Coverage to an Automation Script Prompt
Wrap an existing Python automation script in a pytest suite that mocks subprocess, network, filesystem, and clock side effects so the logic is tested fast and deterministically without touching real systems.
-
Python pytest Fixtures for CLI Integration Tests Prompt
Design pytest fixtures and integration tests that exercise a Python automation CLI end to end with isolated temp filesystems, fake env, and captured output
-
Testing Automation Scripts with bats and pytest Prompt
Add real automated tests to Bash and Python scripts using bats-core and pytest — mocking external commands, asserting exit codes and output, and covering the error paths so refactors stop breaking production jobs.
-
Python CLI Tool with Click Prompt
Design a well-structured Python command-line tool using Click (or argparse) with subcommands, config precedence, good help text, and clean exit codes — packaged so it installs as a console script.
More Bash & Python Automation prompts & error guides
Browse every Bash & Python Automation prompt and troubleshooting guide in one place.
Reading prompts? Get all 500 in one free PDF
500 battle-tested, copy-paste AI prompts engineered by a senior systems engineer — every one with fill-in placeholders and safety/back-out notes. Drop your email and it's yours.
- 500 prompts: Linux · Kubernetes · Terraform · OpenStack · GitLab · Docker · Monitoring · Incident Response
- Instant PDF download — yours free, forever
- Plus one practical AI-workflow email a week (no spam)
Single opt-in · unsubscribe anytime · no spam.