4 SC2314
Joachim Ansorg edited this page 2021-11-15 11:30:03 +01:00

In bats, ! does not cause a test failure.

Problematic code:

#!/usr/bin/env bats

@test "test" {
    # ... code
    ! test_file_exists
    # ... more code
}

Correct code:

#!/usr/bin/env bats

@test "test" {
    # ... code
    run ! test_file_exists
    # ... more code
}

Rationale:

Bats uses set -e and trap ERR to catch test failures as early as possible. Although the return code of a ! negated command is inverted, they will never trigger errexit, due to a bash design decision (see Related Resources). This means that tests which use ! can never fail.

Starting with bats 1.5.0 you can use ! inside run. If you are still using an older bats version, you can rewrite ! <command> to <command> && exit 1.

Exceptions:

The return code of the last command in the test will be the exit code of the test function. This means that you can use ! <command> on the last line of the test and it will still fail appropriately. However, you are encouraged to still use run ! in this case for consistency.