DEV Community

Cover image for Tests Everywhere - Bash
Roger Viñas Alcon
Roger Viñas Alcon

Posted on • Updated on

Tests Everywhere - Bash

GitHub logo rogervinas / tests-everywhere

🤠 Tests, Tests Everywhere!

Bash testing this simple Hello World with BATS

Show me the code

Implementation

  1. Create helloMessage function in hello-message.bash:
function helloMessage() {
  echo "Hello World!"
}
Enter fullscreen mode Exit fullscreen mode
  1. Create helloConsole function in hello-console.bash:
function helloConsole() {
  local text=$1
  echo "$text"
}
Enter fullscreen mode Exit fullscreen mode
  1. Create helloApp function in hello-app.bash:
function helloApp() {
  local messageFn=$1
  local consoleFn=$2
  $consoleFn "$($messageFn)"
}
Enter fullscreen mode Exit fullscreen mode

Note that helloApp function receives the two other functions as parameters and just executes them.

  1. Create a main script hello.bash that just loads the 3 required scripts and executes helloApp passing helloMessage and helloConsole functions as parameters:
source "$(dirname "${BASH_SOURCE[0]}")/hello-message.bash"
source "$(dirname "${BASH_SOURCE[0]}")/hello-console.bash"
source "$(dirname "${BASH_SOURCE[0]}")/hello-app.bash"

helloApp helloMessage helloConsole
Enter fullscreen mode Exit fullscreen mode

Test

Following BATS Tutorial > Your first test ...

  1. For simplicity create all tests in hello.bats file

  2. Configure current directory and load some helper modules in setup function:

setup() {
  load 'test_helper/bats-support/load'
  load 'test_helper/bats-assert/load'

  DIR="$( cd "$( dirname "$BATS_TEST_FILENAME" )" >/dev/null 2>&1 && pwd )"
  PATH="$DIR/../src:$PATH"
}
Enter fullscreen mode Exit fullscreen mode
  1. Test helloMessage function using assert_output helper:
@test "helloMessage should return hello world" {
  source hello-message.bash
  run helloMessage

  assert_output "Hello World!"
}
Enter fullscreen mode Exit fullscreen mode
  1. Test helloApp function:
@test "helloApp should print hello message" {
  # 4.1 Create a helloMessage function mock
  # that will return "Hello Test!"
  function helloMessageMock() {
    echo "Hello Test!"
  }

  # 4.2 Create a helloConsole function mock
  # that will print parameter passed within "helloConsoleMock[...]"
  function helloConsoleMock() {
    local text=$1
    echo "helloConsoleMock[$text]"
  }

  # 4.3 Load helloApp function, the one we want to test
  source hello-app.bash
  # 4.4 Execute helloApp passing mock functions
  run helloApp helloMessageMock helloConsoleMock

  # 4.5 Assert helloConsoleMock has been called once
  # with the returned message by helloMessageMock
  assert_output "helloConsoleMock[Hello Test!]"
}
Enter fullscreen mode Exit fullscreen mode
  1. Test the whole hello.bash script too:
@test "hello.bash should print hello world" {
  run hello.bash

  assert_output "Hello World!"
}
Enter fullscreen mode Exit fullscreen mode
  1. Test output should look like:
hello.bats
 ✓ helloMessage should return hello world
 ✓ helloApp should print hello message
 ✓ hello.bash should print hello world

3 tests, 0 failures
Enter fullscreen mode Exit fullscreen mode

Take a look at the other Libraries and Add-ons that may be useful in the future. For example, there is a couple of bats-mock libraries that can be used to mock programs (but unfortunately not able to mock functions).

Happy Testing! 💙

Top comments (0)