🎓 A1Learner
🔍
0 of 45 lessons completed

Bash TAP Output

TAP output is a plain-text test report made of numbered result lines. Each line starts with ok for a pass or not ok for a failure.

A plan line and two result lines

The plan states two tests. The first result is a pass and the second is a failure.

echo "1..2"
echo "ok 1 - addition"
echo "not ok 2 - subtraction"

A runner prints numbered TAP lines

The runner numbers each check as it runs. The wrong subtraction prints a not ok line in sequence.

n=0
check() {
  n=$((n + 1))
  if [ "$1" = "$2" ]; then
    echo "ok $n - $3"
  else
    echo "not ok $n - $3"
  fi
}
check 5 "$((2 + 3))" "addition"
check 4 "$((5 - 2))" "subtraction"
check "ABC" "$(printf 'abc' | tr 'a-z' 'A-Z')" "uppercase"

The plan can come last

When the test count is known only at the end, the plan prints as a footer. The count still matches the results above it.

n=0
check() {
  n=$((n + 1))
  if [ "$1" = "$2" ]; then
    echo "ok $n - $3"
  else
    echo "not ok $n - $3"
  fi
}
check 5 "$((2 + 3))" "addition"
check 4 "$((5 - 2))" "subtraction"
echo "1..$n"

Example

echo "1..3"
n=0
check() {
  n=$((n + 1))
  if [ "$1" = "$2" ]; then
    echo "ok $n - $3"
  else
    echo "not ok $n - $3"
  fi
}
check 5 "$((2 + 3))" "addition"
check 4 "$((5 - 2))" "subtraction"
check "ABC" "$(printf 'abc' | tr 'a-z' 'A-Z')" "uppercase"

Key points

  • TAP: the Test Anything Protocol, a plain-text format other tools can read.
  • Plan line: 1..3 states how many tests will run, before or after the results.
  • ok line: ok 1 - addition reports that test 1 passed, with its name.
  • not ok line: not ok 2 - subtraction reports that test 2 failed, with its name.
  • Numbering: test numbers start at 1 and rise by one for each result.
  • Machine readable: a TAP consumer can count passes and failures without parsing prose.
  • Honest limit: TAP reports results only; it never explains why a test failed.

In this example, a small TAP runner reports three checks. The plan line first promises three tests. Each check prints one numbered line: ok for a pass and not ok for a failure. The failing subtraction sits between two passes, in order.

Try it Yourself

Edit the code, then press Run.

Running is simulated

Exercise

How does a failing TAP result line look for test 2 named subtraction?