Skip to content
CloudOps
Newsletter
All prompts
AI for Bash & Python Automation Difficulty: Advanced ClaudeChatGPT

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.
Newsletter

Free: the DevOps AI Incident-Triage Cheat Sheet

Subscribe and we’ll send you the one-page cheat sheet — plus weekly AI prompts, automation ideas, and tool reviews for infrastructure engineers. One email a week. No spam, unsubscribe anytime.

  • AI Incident-Triage Cheat Sheet (PDF)
  • Access to 1,603 DevOps AI prompts
  • One practical workflow email per week