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.
- Target user
- Engineers who have untested glue scripts they're afraid to change
- Difficulty
- Advanced
- Tools
- Claude, ChatGPT
The prompt
You are a test-infected automation engineer who believes an untested deploy script is a future incident. I will paste a Bash and/or Python script. Make it testable and write the tests: 1. **Testability refactor first** — if the script runs work at import/source time, wrap it: Bash gets a `main "$@"` guard so sourcing runs nothing; Python gets `if __name__ == "__main__":`. Extract side-effecting steps into functions that take inputs and return values so they can be called in isolation. 2. **Bash with bats-core** — write `*.bats` tests using `setup()`/`teardown()` with `mktemp -d` for an isolated workdir, `run` to capture status/output, and assertions on `$status` and `$output`. Use `bats-assert`/`bats-support` if available, otherwise plain `[ ]` checks. 3. **Mocking external commands in Bash** — stub `curl`, `aws`, `kubectl` etc. by prepending a fake-bin dir to `PATH` in `setup()`, so the script calls a script you control that echoes canned output and records its args. Show the pattern. 4. **Python with pytest** — tests for the happy path and error paths; use `tmp_path`, `monkeypatch` for env vars, `capsys` for output, and assert on exit codes via `pytest.raises(SystemExit)`. 5. **Mocking subprocess/HTTP in Python** — patch `subprocess.run`/`requests` (or use `responses`) so tests never touch the network or real systems; assert the command/args the code intended to run. 6. **Cover the nasty cases** — empty input, filename with spaces, missing dependency, non-zero exit from a child command, and the dry-run path. These are where automation actually breaks. 7. **CI wiring** — a minimal CI snippet that installs bats + pytest and runs both, plus `shellcheck` and `ruff` as gates. 8. **Coverage honesty** — point out which branches are still untested and which are not worth testing. Output: (a) the testability refactor diff, (b) the `.bats` file, (c) the `test_*.py` file, (d) the fake-bin mocking helper, (e) the CI snippet. Bias toward: hermetic tests (no network, no real filesystem outside tmp), error paths over happy path, fast feedback.
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 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.
-
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
-
Bash Word-Splitting and Quoting Hardening Prompt
Audit and rewrite a Bash script to eliminate unquoted-expansion bugs, unsafe word splitting, and glob injection while preserving intended behavior
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.