/bashunit

xUnit unit testing framework for Bash scripts

Primary LanguageShellOtherNOASSERTION

bashunit

bashunit is a unit testing framework for Bash scripts based on xUnit principles.

This is similar to the ShUnit and its successor shUnit2.

Usage

Functions starting with 'test' will be automatically evaluated.

1. Write test cases

testEcho() {
    assertEqual "$(echo foo)" "foo"
    assertReturn "$(echo foo)" 0
}

2. Include this script at the end of your test script

source $(dirname $0)/bashunit.bash

# eof

3. Run test suite

$ ./test_example
testEcho:4:Passed
testEcho:5:Passed
Done. 2 passed. 0 failed. 0 skipped.

The return code is equal to the amount of failed testcases.

Options can be given to the test script:

$ bash ./bashunit.bash
Usage: <testscript> [options...]

Options:
  -v, --verbose  Print expected and provided values
  -s, --summary  Only print summary omitting individual test results
  -q, --quiet    Do not print anything to standard output
  -l, --lineshow  Show failing or skipped line after line number
  -f, --failed   Print only individual failed test results
  -h, --help     Show usage screen

Dependencies

  • Bash (BASH_LINENO)
  • Shell colours

API

  • assert($1)

    $1: Expression

    Assert that a given expression evaluates to true.

  • assertEqual($1, $2)

    $1: Output

    $2: Expected

    Assert that a given output string is equal to an expected string.

  • assertNotEqual($1, $2)

    $1: Output

    $2: Expected

    Assert that a given output string is not equal to an expected string.

  • assertStartsWith($1, $2)

    $1: Output

    $2: Expected

    Assert that a given output string starts with an expected string.

  • assertReturn($1, $2)

    $1: Output

    $2: Expected

    $?: Provided

    Assert that the last command's return code is equal to an expected integer.

  • assertNotReturn($1, $2)

    $1: Output

    $2: Expected

    $?: Provided

    Assert that the last command's return code is not equal to an expected integer.

  • assertGreaterThan($1, $2)

    $1 Output

    $2 Expected

    Assert that a given integer is greater than an expected integer.

  • assertAtLeast($1, $2)

    $1 Output

    $2 Expected

    Assert that a given integer is greater than or equal to an expected integer.

  • assertLessThan($1, $2)

    $1 Output

    $2 Expected

    Assert that a given integer is less than an expected integer.

  • assertAtMost($1, $2)

    $1 Output

    $2 Expected

    Assert that a given integer is less than or equal to an expected integer.

  • skip()

    Skip the current test case.

License

bashunit is licenced under a BSD License.