Assertion library for BATS (Bash Automated Testing System)
Recommended installation is via git submodule. Assuming your project's bats
tests are in test
:
git submodule add https://github.com/jasonkarns/bats-assert test/helpers/assertions
git commit -am 'added bats-assert module'
then in test/test_helper.bash
:
load helpers/assertions/all
(Optionally configure sparse-checkout if you're concerned with all the non-essential files being in your repo)
Also available as an npm module if you're into that sort of thing.
npm install --save-dev bats-assert
then in test/test_helper.bash
:
load ../node_modules/bats-assert/all
flunk: forces a test failure with an optional message
flunk
# or
flunk "expected blue skies"
assert: asserts command returns successfully
assert my-command
assert [ 2 -eq 2 ]
refute: asserts command returns unsuccessfully
refute invalid-command
refute [ 2 -eq 3 ]
assert_success: asserts successful exit $status
with (optional) $output
run my-command
assert_success
# or
assert_success "expected output"
assert_failure: asserts unsuccessful exit $status
with (optional) $output
run my-command
assert_failure
# or
assert_failure "expected output"
assert_equal: asserts equality
actual="$(my-command)"
expected="my results"
assert_equal expected actual
assert_contains: asserts x contains y
assert_contains foobar oo
assert_starts_with: asserts x starts with y
assert_starts_with foobar foo
assert_output: asserts $output
run my-command
assert_output "my results"
assert_output_contains: asserts $output
contains argument
run my-command
assert_output_contains "results"
assert_line: asserts $output
contains given line (at optional line index)
run my-command
assert_line "my results"
# or
assert_line 0 "my results"
refute_line: asserts $output
does not contain given line
run my-command
refute_line "thirsty rando"
Assertion functions taken from the test_helpers of rbenv, ruby-build, and rbenv-aliases. Many thanks to their authors and contributors: Sam Stephenson, Mislav Marohnić, and Tim Pope.