1
0
mirror of https://github.com/sstephenson/bats.git synced 2024-11-17 03:32:27 +01:00

Merge pull request #19 from duggan/internal-skips

Support for TAP compliant skip directive inside test blocks.
This commit is contained in:
Sam Stephenson 2013-10-18 14:14:35 -07:00
commit f4e09aaa2a
4 changed files with 79 additions and 1 deletions

View File

@ -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

View File

@ -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

View File

@ -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" ]
}

7
test/fixtures/bats/skipped.bats vendored Normal file
View File

@ -0,0 +1,7 @@
@test "a skipped test" {
skip
}
@test "a skipped test with a reason" {
skip "a reason"
}