Skip to content

unittest

unittest

Testing library

Simple unittesting library that does simple comparison. Testing library for testing if variables are commands are returning as expected.

Note

rather stable

Example

   L_unittest_eq 1 1

$L_unittest_fails

Integer that increases with every failed test.

$L_unittest_exit_on_error

Set this variable to 1 to exit immediately when a test fails.

$L_unittest_unset_x

Set this varaible to 1 to disable set -x inside L_unittest functions, Set to 0 to don't.

L_unittest_main

Get all functions that start with a prefix specified with -P and execute them one by one.

Options:

  • -h help
  • -P <prefix> Get functions with this prefix to test
  • -r <regex> filter tests with regex
  • -E exit on error

L_unittest_checkexit

Check if command exits with specified exitcode.

Arguments:

  • $1 <int> exit code the command should exit with
  • $@ command to execute

Shellcheck disable= SC2035

L_unittest_success

Check if command exits with 0

Argument: $@ command to execute

L_unittest_failure

Check if command exits with non zero

Argument: $@ command to execute

L_unittest_failure_capture

capture stdout and stderr into variables of a failed command

Arguments:

  • $1 var stdout and stderr output
  • $@ command to execute

L_unittest_cmd

Test execution of a command and capture and test it's stdout and/or stderr output.

Local variables used by this function start with _L_u. Options with L_uopt. This function optionally runs the command in the current shell or not depending on options.

Example

  echo Hello world /tmp/1
  L_unittest_cmd -r 'world' grep world /tmp/1
  L_unittest_cmd -r 'No such file or directory' ! grep something not_existing_file

Options:

  • -h Print this help and exit.
  • -c Run in current execution environment, instead of using a subshell.
  • -i Invert exit status. You can also use ! or L_not in front of the command.
  • -I Do not close stdin <&1 . By default it is closed.
  • -f Expect the command to fail. Equal to -i -j -N.
  • -N Redirect stdout of the command to >/dev/null.
  • -j Redirect stderr to stdout of the command. 2>&1
  • -x Run the command inside set -x
  • -X Do not modify set -x
  • -v <var> Capture stdout of the command into this variable. Use -j to capture also stderr.
  • -r <regex> Compare output of the command with this regex.
  • -o <str> Compare output of the command with this string.
  • -e <int> Command should exit with this exit status (default: 0)

Argument: $@

Command to execute.

If a command starts with !, this implies -i and, if one of -v -r -o option is used, it implies -j.

L_unittest_vareq

Test if a variable has specific value.

Arguments:

  • $1 variable nameref
  • $2 value

L_unittest_eq

Test if two strings are equal.

Arguments:

  • $1 one string
  • $2 second string

L_unittest_arreq

Test if array is equal to elements.

Arguments:

  • $1 array variable
  • $@ values

L_unittest_ne

Test two strings are not equal.

Arguments:

  • $1 one string
  • $2 second string

L_unittest_regex

test if a string matches regex

Arguments:

  • $1 string
  • $2 regex

L_unittest_contains

Test if a string contains other string.

Arguments:

  • $1 string
  • $2 needle