/bats-assert

Assertion library for BATS (Bash Automated Testing System)

Primary LanguageShellMIT LicenseMIT

DEPRECATED in favor of jasonkarns/bats-assert-1, itself forked from ztombol/bats-assert while ztombol has gone quiet. Eventually hoping for it to live under the bats-core org: bats-core/bats-core#44

bats-assert

Assertion library for BATS (Bash Automated Testing System)

Installation

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@1.1.1

then in test/test_helper.bash:

load ../node_modules/bats-assert/all

Assertion API

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

refute_contains

asserts x does not contain y

refute_contains foobar baz

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"

refute_output_contains

asserts $output does not contain argument

run my-command

refute_output_contains "unicorn"

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"

Credits

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.