Skip to content
shellhacks.dev
Testing

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.

2 min read
Summarize in AI

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.

Shell
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: $?"
Text
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: 0

skip 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.

Share

More snippets

Testing

Choose the shell for ShellCheck

Tell ShellCheck which shell a script targets with -s bash or -s sh. Without a shebang it cannot tell, and as sh it flags every Bash-only feature as SC3xxx.

2 min read
Testing

Check status in Bats

Check a command's exit code in Bats with run and $status. run never fails the test by itself, and Bats 1.5 added run -N to assert the status in one line.

2 min read
Testing

Load helpers in Bats

Share code between Bats tests with load helpers, which reads helpers.bash from the test file's directory. A plain source breaks when bats runs from elsewhere.

2 min read

Get new posts by email

One email when a new post is published. No spam.

Unsubscribe in one click. No spam, ever.