Bash & Shell Scripting (50 Qs)•Part 3: Advanced Text Processing & Production Internals (Senior / Lead)
Testing Bash Scripts with BATS (Bash Automated Testing System): Assertions and mocks.
senior LevelCode ExampleTesting
In Simple Words: BATS is an automated testing framework for Bash scripts providing unit test structures:
bash
# test_math.bats
setup() {
load 'test_helper/bats-support/load'
load 'test_helper/bats-assert/load'
source ./calculator.sh
}
@test "add function correctly sums two positive integers" {
run add 5 10
assert_success
assert_output "15"
}
@test "divide function throws error on division by zero" {
run divide 10 0
assert_failure
assert_output --partial "Division by zero"
}