diff --git a/README.md b/README.md index 2e64fe7..19cdee3 100644 --- a/README.md +++ b/README.md @@ -111,6 +111,43 @@ will source the script `test/test_helper.bash` in your test file. This can be useful for sharing functions to set up your environment or load fixtures. +### The _skip_ directive + +Tests can be skipped over by using the `skip` directive at the point in +a test you wish to skip. + +```bash +@test "A test I don't want to execute for now" { + skip + run foo + [ "$status" -eq 0 ] +} +``` + +Optionally, a reason for skipping can be included: + +```bash +@test "A test I don't want to execute for now" { + skip "This command will return zero soon, but not now" + run foo + [ "$status" -eq 0 ] +} +``` + +Or you can skip conditionally: + +```bash +@test "A test which should run" { + + if [ foo != bar ]; then + skip "foo isn't bar" + fi + + run foo + [ "$status" -eq 0 ] +} +``` + ### Special variables There are several global variables you can use to introspect on Bats diff --git a/libexec/bats-exec-test b/libexec/bats-exec-test index d4c21b3..8f4396f 100755 --- a/libexec/bats-exec-test +++ b/libexec/bats-exec-test @@ -57,6 +57,12 @@ teardown() { true } +skip() { + BATS_TEST_SKIPPED=${1:-1} + BATS_TEST_COMPLETED=1 + exit 0 +} + bats_test_info() { BATS_TEST_DESCRIPTION="$1" } @@ -85,15 +91,24 @@ bats_teardown_trap() { bats_exit_trap() { local status + local skipped trap - err exit + skipped="" + if [ -n "$BATS_TEST_SKIPPED" ]; then + skipped=" # skip" + if [ "1" != "$BATS_TEST_SKIPPED" ]; then + skipped+=" ($BATS_TEST_SKIPPED)" + fi + fi + if [ -z "$BATS_TEST_COMPLETED" ]; then echo "not ok $BATS_TEST_NUMBER $BATS_TEST_DESCRIPTION" >&3 echo "# $BATS_TEST_FILENAME:$BATS_LINE_NUMBER" >&3 sed -e "s/^/# /" < "$BATS_OUT" >&3 status=1 else - echo "ok $BATS_TEST_NUMBER $BATS_TEST_DESCRIPTION" >&3 + echo "ok ${BATS_TEST_NUMBER}${skipped} ${BATS_TEST_DESCRIPTION}" >&3 status=0 fi diff --git a/test/bats.bats b/test/bats.bats index fe24cd7..245fab1 100755 --- a/test/bats.bats +++ b/test/bats.bats @@ -110,3 +110,22 @@ fixtures bats run bats "$FIXTURE_ROOT/dos_line.bats" [ $status -eq 0 ] } + +@test "a skipped test" { + skip + run bats + [ $status -eq 0 ] +} + +@test "a skipped test with a reason" { + skip "a reason for skipping" + run bats + [ $status -eq 0 ] +} + +@test "skipped test output" { + run bats "$FIXTURE_ROOT/skipped.bats" + [ $status -eq 0 ] + [ "${lines[1]}" = "ok 1 # skip a skipped test" ] + [ "${lines[2]}" = "ok 2 # skip (a reason) a skipped test with a reason" ] +} diff --git a/test/fixtures/bats/skipped.bats b/test/fixtures/bats/skipped.bats new file mode 100644 index 0000000..5b0d61b --- /dev/null +++ b/test/fixtures/bats/skipped.bats @@ -0,0 +1,7 @@ +@test "a skipped test" { + skip +} + +@test "a skipped test with a reason" { + skip "a reason" +}