Fixtures and regression testing
A fixture is controlled starting data for a test: files, arguments, environment and expected results. A regression test reproduces a known failure and should reject the buggy implementation, not just accept the corrected one. Use separate disposable directories, a controlled locale and known inputs. Check zero arguments, empty values, spaces, wildcard characters, missing dependencies and nonzero statuses as appropriate. Do not derive expected results by running the same code under test; that can reproduce its bug. Separate setup, action, assertion and cleanup. Capture stdout, stderr and status independently; verify file contents or absence where relevant. A small fixed reproduction is easier to debug than a whole production script. For a missing dependency use a controlled PATH rather than uninstalling tools. Clean only resources owned by the fixture. Check fixture creation, command completion and comparison status independently. A missing output file must not count as a successful rejection of an incorrect implementation.
Practical examples
Run each example as a separate script in a fresh noninteractive Bash 5.2 process and empty practice directory. Tests use C locale; do not enter expected output as code.
Reproduce a wildcard regression
: > item.txt || exit 1bad() { printf '[%s]\n' $1; }good() { printf '[%s]\n' "$1"; }printf '[*]\n' > expected.txt || exit 1good '*' > good.txt || exit 1bad '*' > bad.txt || exit 1cmp -s good.txt expected.txt || exit 1cmp -s bad.txt expected.txtrc=$?[[ $rc == 1 ]] || exit 1printf 'good accepted; bad rejected\n' Expected stdout:
good accepted; bad rejectedThe test uses fixed input and rejects a known incorrect implementation.
Isolate a missing dependency
mkdir empty-path || exit 1if PATH="$PWD/empty-path" command -v bm_demo_missing >/dev/null; then printf 'unexpected dependency\n' exit 1else printf 'missing dependency detected\n'fiExpected stdout:
missing dependency detectedThe unique synthetic name and empty PATH directory make the absence deliberate.
Verify cleanup of an owned file
( (set -C; printf 'fixture\n' > owned.tmp) || exit 1 trap 'rm -f -- owned.tmp' EXIT exit 4)rc=$?[[ $rc == 4 && ! -e owned.tmp && ! -L owned.tmp ]] || exit 1printf 'cleanup verified\n' Expected stdout:
cleanup verifiedThe parent verifies both the original failure status and file removal.
Common mistakes
Do not test in a real project directory or depend on ambient glob matches. Do not count a test as useful if it also accepts the known broken solution. Never delete an unchecked variable path.
Practice
Create a fixture containing a filename with spaces and a matching wildcard file. Compare a correct argument-preserving implementation with an unquoted version. Show that the test rejects the latter and that repeated runs produce the same result.
Next: Processes, signals and concurrency