mirror of
https://github.com/sstephenson/bats.git
synced 2024-11-17 11:42:33 +01:00
Merge pull request #19 from duggan/internal-skips
Support for TAP compliant skip directive inside test blocks.
This commit is contained in:
commit
f4e09aaa2a
37
README.md
37
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
|
can be useful for sharing functions to set up your environment or load
|
||||||
fixtures.
|
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
|
### Special variables
|
||||||
|
|
||||||
There are several global variables you can use to introspect on Bats
|
There are several global variables you can use to introspect on Bats
|
||||||
|
|
|
@ -57,6 +57,12 @@ teardown() {
|
||||||
true
|
true
|
||||||
}
|
}
|
||||||
|
|
||||||
|
skip() {
|
||||||
|
BATS_TEST_SKIPPED=${1:-1}
|
||||||
|
BATS_TEST_COMPLETED=1
|
||||||
|
exit 0
|
||||||
|
}
|
||||||
|
|
||||||
bats_test_info() {
|
bats_test_info() {
|
||||||
BATS_TEST_DESCRIPTION="$1"
|
BATS_TEST_DESCRIPTION="$1"
|
||||||
}
|
}
|
||||||
|
@ -85,15 +91,24 @@ bats_teardown_trap() {
|
||||||
|
|
||||||
bats_exit_trap() {
|
bats_exit_trap() {
|
||||||
local status
|
local status
|
||||||
|
local skipped
|
||||||
trap - err exit
|
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
|
if [ -z "$BATS_TEST_COMPLETED" ]; then
|
||||||
echo "not ok $BATS_TEST_NUMBER $BATS_TEST_DESCRIPTION" >&3
|
echo "not ok $BATS_TEST_NUMBER $BATS_TEST_DESCRIPTION" >&3
|
||||||
echo "# $BATS_TEST_FILENAME:$BATS_LINE_NUMBER" >&3
|
echo "# $BATS_TEST_FILENAME:$BATS_LINE_NUMBER" >&3
|
||||||
sed -e "s/^/# /" < "$BATS_OUT" >&3
|
sed -e "s/^/# /" < "$BATS_OUT" >&3
|
||||||
status=1
|
status=1
|
||||||
else
|
else
|
||||||
echo "ok $BATS_TEST_NUMBER $BATS_TEST_DESCRIPTION" >&3
|
echo "ok ${BATS_TEST_NUMBER}${skipped} ${BATS_TEST_DESCRIPTION}" >&3
|
||||||
status=0
|
status=0
|
||||||
fi
|
fi
|
||||||
|
|
||||||
|
|
|
@ -110,3 +110,22 @@ fixtures bats
|
||||||
run bats "$FIXTURE_ROOT/dos_line.bats"
|
run bats "$FIXTURE_ROOT/dos_line.bats"
|
||||||
[ $status -eq 0 ]
|
[ $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
7
test/fixtures/bats/skipped.bats
vendored
Normal file
|
@ -0,0 +1,7 @@
|
||||||
|
@test "a skipped test" {
|
||||||
|
skip
|
||||||
|
}
|
||||||
|
|
||||||
|
@test "a skipped test with a reason" {
|
||||||
|
skip "a reason"
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user