Skip a Bats test
Skip a Bats test with skip or skip "reason", which can be called in the middle of a test. A skipped test counts as passed, and the reason shows in the output.
A test depends on a tool or a setting that is not on every machine, and it should step aside there without failing the run.
cat > skip.bats <<'EOT'
@test "always runs" {
true
}
@test "skipped with a reason" {
skip "needs docker"
false
}
@test "skipped without a reason" {
skip
}
@test "skipped when a tool is missing" {
command -v nonexistent-tool > /dev/null || skip "nonexistent-tool is not installed"
nonexistent-tool
}
EOT
bats skip.bats
echo "bats exit: $?"1..4
ok 1 always runs
ok 2 skipped with a reason # skip needs docker
ok 3 skipped without a reason # skip
ok 4 skipped when a tool is missing # skip nonexistent-tool is not installed
bats exit: 0skip stops the test at that line and marks it skipped, with the optional text as the reason, and Bats reports the run as successful when nothing failed. Because it can be called anywhere in the test body, or in setup, it works for runtime conditions like the last test, which looks for the tool first. The output lists each reason, and the final summary counts the skipped tests separately. See the Bats documentation.
A test that always skips hides itself, so a skip with no condition should have a ticket or a date behind it. Anything before skip has already run, including side effects such as created files. In CI, treat a growing count of skips as a signal, since a run in which half the tests skip still exits 0.