Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions docs/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ The format is based on [Keep a Changelog][kac] and this project adheres to
(18.04 -> 22.04) (#630)
* add `/usr/lib/bats` as default value for `BATS_LIB_PATH` (#628)
* fix unset variable in `bats-formatter-junit` when `setup_file` fails (#632)
* unify error behavior of `teardown`/`teardown_file`/`teardown_suite` functions:
only fail via return code, not via ERREXIT (#633)

#### Documentation

Expand Down
36 changes: 36 additions & 0 deletions docs/source/writing-tests.md
Original file line number Diff line number Diff line change
Expand Up @@ -268,6 +268,42 @@ teardown_suite # from setup_suite.bash
</details>
<!-- markdownlint-enable MD033 -->

Note that the `teardown*` functions can fail a test, if their return code is nonzero.
This means, using `return 1` or having the last command in teardown fail, will
fail the teardown. Unlike `@test`, failing commands within `teardown` won't
trigger failure as ERREXIT is disabled.

<!-- markdownlint-disable MD033 -->
<details>
<summary>Example of different teardown failure modes</summary>

```bash
teardown() {
false # this will fail the test, as it determines the return code
}

teardown() {
false # this won't fail the test ...
echo some more code # ... and this will be executed too!
}

teardown() {
return 1 # this will fail the test, but the rest won't be executed
echo some more code
}

teardown() {
if true; then
false # this will also fail the test, as it is the last command in this function
else
true
fi
}
```

</details>
<!-- markdownlint-enable MD033 -->

## `bats_require_minimum_version <Bats version number>`

Added in [v1.7.0](https://github.com/bats-core/bats-core/releases/tag/v1.7.0)
Expand Down
13 changes: 9 additions & 4 deletions libexec/bats-core/bats-exec-suite
Original file line number Diff line number Diff line change
Expand Up @@ -296,14 +296,19 @@ bats_suite_exit_trap() {
}

bats_run_teardown_suite() {
local bats_teardown_suite_status=0
# avoid being called twice, in case this is not called through bats_teardown_suite_trap
# but from the end of file
trap bats_suite_exit_trap EXIT
set -eET
BATS_TEARDOWN_SUITE_COMPLETED=
teardown_suite 2>&1
BATS_TEARDOWN_SUITE_COMPLETED=1
set +ET
teardown_suite 2>&1 || bats_teardown_suite_status=$?
if (( bats_teardown_suite_status == 0 )); then
BATS_TEARDOWN_SUITE_COMPLETED=1
elif [[ -n "${BATS_SETUP_SUITE_COMPLETED:-}" ]]; then
BATS_DEBUG_LAST_STACK_TRACE_IS_VALID=1
BATS_ERROR_STATUS=$bats_teardown_suite_status
return $BATS_ERROR_STATUS
fi
}

bats_teardown_suite_trap() {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
setup_suite() {
:
}

teardown_suite() {
echo "teardown_suite before" >&2
return 1
echo "teardown_suite after" >&2
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
@test test { :; }
17 changes: 13 additions & 4 deletions test/suite_setup_teardown.bats
Original file line number Diff line number Diff line change
Expand Up @@ -91,12 +91,21 @@ setup() {
[ "${lines[5]}" == "# \`false' failed" ]
}

@test "failure in teardown_suite is reported and fails test suite, remaining code is skipped" {
run ! bats "$FIXTURE_ROOT/failure_in_teardown_suite/"
@test "midway failure in teardown_suite does not fail test suite, remaining code is executed" {
run -0 bats "$FIXTURE_ROOT/failure_in_teardown_suite/"
[ "${lines[2]}" == "teardown_suite before" ]
[ "${lines[3]}" == "teardown_suite after" ]
[ "${#lines[@]}" -eq 4 ]
}

@test "nonzero return in teardown_suite does fails test suite" {
run -1 bats "$FIXTURE_ROOT/return_nonzero_in_teardown_suite/"
[ "${lines[2]}" == "teardown_suite before" ]
[ "${lines[3]}" == "not ok 2 teardown_suite" ]
[ "${lines[4]}" == "# (from function \`teardown_suite' in test file $RELATIVE_FIXTURE_ROOT/failure_in_teardown_suite/setup_suite.bash, line 7)" ]
[ "${lines[5]}" == "# \`false' failed" ]
[ "${lines[4]}" == "# (from function \`teardown_suite' in test file $RELATIVE_FIXTURE_ROOT/return_nonzero_in_teardown_suite/setup_suite.bash, line 7)" ]
[ "${lines[5]}" == "# \`return 1' failed" ]
[ "${lines[6]}" == "# bats warning: Executed 2 instead of expected 1 tests" ]
[ "${#lines[@]}" -eq 7 ]
}

@test "stderr from setup/teardown_suite does not overtake stdout" {
Expand Down