Free preview.You're sampling one lesson — enroll free to unlock all 8 lessons and track your progress.
Enroll free
lesson

Why and What to Test

Why and What to Test

In this lesson — part of Testing Fundamentals — you'll learn why and what to test in Bash and why it matters in real work.

Why it matters

Tests prove your code works and keep it working as you change it.

Key ideas

  • What to test
  • Arrange-Act-Assert
  • Running a test suite
  • Good vs. brittle tests

In practice

Here's how it looks in idiomatic Bash:

assert_eq() {                 # tiny test helper
  [[ $1 == "$2" ]] && return 0
  echo "FAIL: '$1' != '$2'" >&2; return 1
}
add() { echo $(( $1 + $2 )); }
assert_eq "$(add 2 3)" 5 && echo "PASS add"
# real projects use the Bats framework:  @test "adds" { ... }

Bash note: Unit testing in Bash typically means the Bats framework, whose @test blocks and run/$status/$output helpers isolate each case — hand-rolled assert_eq works but won't sandbox set -e or failing subshells the way Bats does.

Try it yourself

Exercise: In Bash, write three tests for a function that reverses a string.

Recap

You now understand why and what to test and can apply it in Bash. Mark this lesson complete and continue to the next one.