Skip to main content

โœ… Assertions

Assertions are called through the assert helper:

assert <value> <assertion> [<comparison>...]

If an assertion fails, the test stops immediately and is marked as failed.

String assertionsโ€‹

same_asโ€‹

Passes when $value is equal to $comparison (string equality).

assert "$output" same_as 'expected string'

different_toโ€‹

Passes when $value is not equal to $comparison.

assert "$result" different_to 'forbidden'

containsโ€‹

Passes when $value contains $comparison as a substring.

assert "$output" contains 'hello'

does_not_containโ€‹

Passes when $value does not contain $comparison.

assert "$output" does_not_contain 'error'

is_substring_ofโ€‹

Passes when $value is a substring of $comparison (reverse of contains).

assert 'hell' is_substring_of 'hello world'

is_not_substring_ofโ€‹

Passes when $value is not a substring of $comparison.

assert 'xyz' is_not_substring_of 'hello world'

matchesโ€‹

Passes when $value matches the regex $pattern.

assert "$output" matches '^[0-9]+$'

does_not_matchโ€‹

Passes when $value does not match the regex $pattern.

assert "$output" does_not_match '^Error'

is_emptyโ€‹

Passes when $value is an empty string or unset.

assert "$output" is_empty

is_not_emptyโ€‹

Passes when $value is non-empty.

assert "$result" is_not_empty

Integer assertionsโ€‹

equalsโ€‹

Passes when the two integers are equal (-eq).

assert $state equals 0

not_equal_toโ€‹

Passes when the two integers are not equal (-ne).

assert $count not_equal_to 0

is_positiveโ€‹

Passes when the integer is greater than zero.

assert $count is_positive

is_negativeโ€‹

Passes when the integer is less than zero.

assert $diff is_negative

is_greater_thanโ€‹

Passes when $value is greater than $comparison (-gt).

assert $count is_greater_than 5

is_less_thanโ€‹

Passes when $value is less than $comparison (-lt).

assert $count is_less_than 100

Array assertionsโ€‹

inโ€‹

Passes when $value is found in the array.

local -a fruits=(apple banana cherry)
assert 'banana' in "${fruits[@]}"

not_inโ€‹

Passes when $value is not found in the array.

assert 'grape' not_in "${fruits[@]}"

Hash assertionsโ€‹

is_key_inโ€‹

Passes when $value is a key in the associative array.

local -A config=(host localhost port 5432)
assert 'host' is_key_in "${(kv)config[@]}"

is_not_key_inโ€‹

Passes when $value is not a key in the hash.

assert 'password' is_not_key_in "${(kv)config[@]}"

is_value_inโ€‹

Passes when $value is a value in the associative array.

assert 'localhost' is_value_in "${(kv)config[@]}"

is_not_value_inโ€‹

Passes when $value is not a value in the hash.

assert 'remotehost' is_not_value_in "${(kv)config[@]}"

Filesystem assertionsโ€‹

All filesystem assertions accept relative paths (resolved against the test directory) or absolute paths.

existsโ€‹

Passes when the path exists (file, directory, or symlink).

assert 'output.txt' exists

is_fileโ€‹

Passes when the path exists and is a regular file.

assert 'output.txt' is_file

is_dirโ€‹

Passes when the path exists and is a directory.

assert '_output' is_dir

Passes when the path exists and is a symbolic link.

assert 'current' is_link

is_readableโ€‹

Passes when the path exists and is readable by the current user.

assert 'config.yml' is_readable

is_writableโ€‹

Passes when the path exists and is writable.

assert 'output.txt' is_writable

is_executableโ€‹

Passes when the path exists and is executable.

assert 'myscript.zsh' is_executable