1
0
mirror of https://github.com/sstephenson/bats.git synced 2024-12-27 06:59:45 +01:00

Make dependencies transitive/inherited

This commit is contained in:
Francois Laupretre 2018-05-21 14:59:00 +02:00
parent ac619feb58
commit 3e28a098ad
2 changed files with 18 additions and 11 deletions

View File

@ -195,14 +195,14 @@ same name to different tests,
### Test dependencies ### Test dependencies
Some tests may have one or more tests as pre-requisites, meaning that tests Some tests may have one or more tests as pre-requisites, meaning that tests
are irrelevant if the pre-requisites fail. A common case is test A checking that a are irrelevant if the pre-requisites fail. A common case is test A checking that
given service is running, while test B and C use this service to retrieve more service 'foo' is running, while test B and C use this service to retrieve more
information. If service is not running, we know for sure that test B and C will information. If service 'foo' is not running, we know for sure that tests B and C will
fail. In order to avoid a bunch of errors when such dependency exists, you can fail. In such cases, readability is much better if we display test A as failed
instruct bash to skip test B and C if A fails. and tests B and C as skipped.
In order to implement such a dependency, you will use the 'bats_test_succeeds()' In order to implement such a dependency, you will use the 'bats_test_succeeds()'
function. This function returns true (0) if all its argument every test names function. This function returns true (0) if all the test names given as arguments
succeeded, and false if any of them failed. succeeded, and false if any of them failed.
Example: Example:
@ -227,12 +227,14 @@ Example:
Notes: Notes:
* You cannot define cross-file dependencies. The test(s) you refer too must * You cannot define cross-file dependencies. The test(s) you refer to must
be located in the same test file. be located in the same test file.
* As tests are executed in the order they appear in the file, you can only depend * As tests are executed in the order they appear in the file, you can only depend
on tests that appear in the test file BEFORE/ABOVE the current test. If this is on tests that appear in the test file BEFORE/ABOVE the current test. If this is
not the case, a warning message is issued on stderr and the parent test is not the case, a warning message is issued on stderr and the parent test is
supposed to be successful (depending test will run). supposed to be successful (depending test will run).
* Dependencies are transitive/inherited. If a test depends on a test skipped for any reason,
it will be skipped too, along with its descendants.
* You need to set explicit names for the tests you want to create dependencies on. * You need to set explicit names for the tests you want to create dependencies on.
### `setup` and `teardown`: Pre- and post-test hooks ### `setup` and `teardown`: Pre- and post-test hooks

View File

@ -241,7 +241,7 @@ bats_teardown_trap() {
} }
bats_exit_trap() { bats_exit_trap() {
local status local status # Exit code: 0=OK, 1=error, 2=skipped
local skipped local skipped
trap - err exit trap - err exit
@ -262,6 +262,7 @@ bats_exit_trap() {
else else
echo "ok ${BATS_TEST_NUMBER}${skipped} ${BATS_TEST_DESCRIPTION}" >&3 echo "ok ${BATS_TEST_NUMBER}${skipped} ${BATS_TEST_DESCRIPTION}" >&3
status=0 status=0
[ -n "$BATS_TEST_SKIPPED" ] && status=2
fi fi
rm -f "$BATS_OUT" rm -f "$BATS_OUT"
@ -290,7 +291,7 @@ bats_test_succeeds() {
if [ -z $status ] ; then if [ -z $status ] ; then
echo "Warning: $name: Cannot check status as test didn't run yet" >&2 echo "Warning: $name: Cannot check status as test didn't run yet" >&2
else else
let ret+=$status [ $status = 0 ] && ret=1
fi fi
fi fi
done done
@ -299,15 +300,19 @@ bats_test_succeeds() {
} }
bats_perform_tests() { bats_perform_tests() {
local ret status test_number test_name
ret=0
echo "1..$#" echo "1..$#"
test_number=1 test_number=1
status=0 status=0
for test_name in "$@"; do for test_name in "$@"; do
"$0" $BATS_EXTENDED_SYNTAX "$BATS_TEST_FILENAME" "$test_name" "$test_number" || status=1 "$0" $BATS_EXTENDED_SYNTAX "$BATS_TEST_FILENAME" "$test_name" "$test_number" || status=$?
[ $status = 1 ] && ret=1 # Skip -> OK
BATS_TEST_RESULTS[$test_number]=$status BATS_TEST_RESULTS[$test_number]=$status
let test_number+=1 let test_number+=1
done done
exit "$status" exit "$ret"
} }
bats_perform_test() { bats_perform_test() {