1
0
mirror of https://github.com/sstephenson/bats.git synced 2024-12-26 06:29:47 +01:00

Test (and fix) that teardown runs once after each test

This commit is contained in:
Sam Stephenson 2011-12-28 21:20:43 -06:00
parent 6e65ef449b
commit 43d1972b0e
4 changed files with 39 additions and 5 deletions

View File

@ -52,6 +52,12 @@ bats_test_function() {
BATS_TEST_NAMES["${#BATS_TEST_NAMES[@]}"]="$test_name"
}
bats_teardown_trap() {
trap bats_exit_trap err exit
teardown
bats_exit_trap
}
bats_exit_trap() {
trap - err exit
if [ -z "$BATS_TEST_COMPLETED" ]; then
@ -84,7 +90,7 @@ bats_perform_test() {
fi
BATS_TEST_COMPLETED=""
trap bats_exit_trap err exit
trap bats_teardown_trap err exit
setup
"$BATS_TEST_NAME"
BATS_TEST_COMPLETED=1

View File

@ -55,6 +55,15 @@ teardown() {
@test "setup is run once before each test" {
rm -f "$TMP/setup.log"
run bats "$FIXTURE_ROOT/setup.bats"
[ $status -eq 0 ]
run cat "$TMP/setup.log"
[ ${#lines[@]} -eq 3 ]
}
@test "teardown is run once after each test, even if it fails" {
rm -f "$TMP/teardown.log"
run bats "$FIXTURE_ROOT/teardown.bats"
[ $status -eq 1 ]
run cat "$TMP/teardown.log"
[ ${#lines[@]} -eq 3 ]
}

View File

@ -1,15 +1,17 @@
LOG="$TMP/setup.log"
setup() {
echo "$BATS_TEST_NAME" >> "$TMP/setup.log"
echo "$BATS_TEST_NAME" >> "$LOG"
}
@test "one" {
true
[ "$(tail -n 1 "$LOG")" = "test_one" ]
}
@test "two" {
true
[ "$(tail -n 1 "$LOG")" = "test_two" ]
}
@test "three" {
true
[ "$(tail -n 1 "$LOG")" = "test_three" ]
}

17
test/fixtures/teardown.bats vendored Normal file
View File

@ -0,0 +1,17 @@
LOG="$TMP/teardown.log"
teardown() {
echo "$BATS_TEST_NAME" >> "$LOG"
}
@test "one" {
true
}
@test "two" {
false
}
@test "three" {
true
}