lehmannro / assert.sh

bash unit testing framework
GNU Lesser General Public License v3.0
487 stars 57 forks source link

How to test stderr? #24

Open holgerbrandl opened 7 years ago

holgerbrandl commented 7 years ago

Could you give any advice on how to test stderr? E.g. for assert_raises I'd love to assert on some expected error message.

holgerbrandl commented 7 years ago

Figured it out myself. We can just add small bash functions helpers to the test suite test more complex behaviors in a concise way:

. assert.sh

assert_statment(){
    # usage cmd exp_stout exp_stder exp_exit_code
    assert "$1" "$2"
    assert "( $1 ) 2>&1 >/dev/null" "$3"
    assert_raises "$1" "$4"
}

assert_statment "echo foo; echo bar  >&2; exit 1" "foo" "bar" 1