Skip to main content
SHELL-SCRIPTING6 MIN READ

Test shell behavior with Bats before cron does

Write a small Bats test that verifies shell script status, output, and cleanup behavior.

backup.sh should require S3_BUCKET, print diagnostics to stderr, and avoid side effects in dry-run mode. Bats contract testing: run the script, assert exit status, assert stdout/stderr, and isolate side effects with setup/teardown. Do not test only the happy path. Shell scripts most often hurt teams when failure still looks successful. Fixture In setup(), create a temp directory and export PATH so the test invokes the script under test. The test should not depend on where the developer launched Bats. Bats exposes helpers such as the test filename so setup can anchor paths. Run Use run ./backup.sh --dry-run fixture/ instead of…

Read the full lesson

Sign up free — one personalized lesson every day, matched to your role and goals.

Already have an account? Sign in

← Back to library
Contact us