Skip to content

Latest commit

 

History

History
55 lines (39 loc) · 1.88 KB

20-Bats-BestPractices.md

File metadata and controls

55 lines (39 loc) · 1.88 KB

Bats best practices

1. use of default temp directory created by bats

Instead of creating yourself your temp directory, you can use the special variable BATS_TEST_TMPDIR, this directory is automatically destroyed at the end of the test except if the option --no-tempdir-cleanup is provided to bats command.

Exception: if you are testing bash traps, you would need to create your own directories to avoid unexpected errors.

2. avoid boilerplate code

using this include, includes most of the features needed when using bats

# shellcheck source=src/batsHeaders.sh
source "$(cd "${BATS_TEST_DIRNAME}/.." && pwd)/batsHeaders.sh"

It sets those bash features:

  • set -o errexit
  • set -o pipefail

It imports several common files like some additional bats features.

And makes several variables available:

3. Override an environment variable when using bats run

SUDO="" run Linux::Apt::update

4. Override a bash framework function

using stub is not possible because it does not support executable with special characters like ::. So the solution is just to override the function inside your test function without importing the original function of course. In tearDown method do not forget to use unset -f yourFunction