by lehmannro

lehmannro /

bash unit testing framework

427 Stars 56 Forks Last release: Not found GNU Lesser General Public License v3.0 97 Commits 4 Releases

Available items

No Items, yet!

The developer of this repository has not created any items for sale yet. Need a bug fixed? Help with integration? A different license? Create a request here: is test-driven development in the Bourne again shell.

:Version: 1.1 :Author: Robert Lehmann :License: LGPLv3

.. image:: :target:





echo test
is expected to write "test" on stdout assert "echo test" "test" #
seq 3
is expected to print "1", "2" and "3" on different lines assert "seq 3" "1\n2\n3" # exit code of
is expected to be 0 assertraises "true" # exit code of
is expected to be 1 assert
raises "false" 1 # end of test suite assert_end examples

If you had written the above snippet into
you could invoke it without any extra hassle::

$ ./ all 4 examples tests passed in 0.014s.

Watch out to have
executable (
chmod +x
), otherwise you need to invoke it with

Now, we will add a failing test case to our suite::

# expect

exit 127
to terminate with code 128 assert_raises "exit 127" 128

Remember to insert test cases before

(or write another
to the end of your file). Otherwise test statistics will be omitted.

When run, the output is::

test #5 "exit 127" failed: program terminated with code 127 instead of 128 1 of 5 examples tests failed in 0.019s.

The overall status code is 1 (except if you modified the exit code manually)::

$ bash ... $ echo $? 1


  • lightweight interface:
  • minimal setup -- source
    and you're done
  • test grouping in individual suites
  • time benchmarks with real-time display of test progress
  • run all tests, stop on first failure, or collect numbers only
  • automatically set the exit status of the test script
  • skip individual tests

Use case

You wrote an application. Following sane development practices, you want to protect yourself against introducing errors with a test suite. Even though most languages have excellent testing tools, modifying process state (input

, command line arguments
, environment variables) is awkard in most languages. The shell was made to do just that, so why don't run the tests in your shell?


You can easily install the latest release (or any other version)::


Use the following command to grab a snapshot of the current development version::


There is no additional build/compile step except for changing permissions (

chmod +x
) depending on the way you have chosen to install



package manager allows you to install locally::

bpkg install lehmannro/

(Watch out to

source deps/assert/

If you want to install globally, for your whole system, use::

bpkg install lehmannro/ -g


  • assert  [stdout] [stdin]

Check for an expected output when running your command.

supports all control sequences
echo -e
interprets, eg.
for a newline. The default
is assumed to be empty.
  • assert_raises  [exitcode] [stdin]


terminated with the expected status code. The default
is assumed to be 0.
  • assert_end [suite]

Finalize a test suite and print statistics.

  • skip

Unconditionally skip the following test case. The skipped test case is exempt from any test diagnostics (ie., not accounted for in the total number of tests.)

  • skip_if 

Skip the following test case if

exits successfully. (
disclaimer applies.) Use this if you want to run a test only if some precondition is met, eg. the test needs root privileges or network access.

Command line options

See --help
for command line options on test runners.

-v, --verbose Generate real-time output for every individual test run. -x, --stop Stop running tests after the first failure. (Default: run all tests.) -i, --invariant Do not measure runtime for suites. Useful mainly to parse test output. -d, --discover Collect test suites and number of tests only; don't run any tests. -c, --continue Do not modify exit code depending on overall suite status. -h Show brief usage information and exit. --help Show usage manual and exit.

Environment variables

================= ==================== variable corresponding option ================= ====================

================= ====================


1.1 * Added

commands. * Added support for
set -e
environments (closes
, thanks David Schoen.) * Modified exit code automatically in case any test failed in the suite. * Added
flag to avoid tinkering with the exit code. * Removed
dependency (closes
, thanks Maciej Żok.) * Added installation instructions for
_ (closes
_, thanks Joseph Werle.)

1.0.2 * Fixed Mac OS compatibility (closes


1.0.1 * Added support for

set -u
environments (closes
_.) * Fixed several leaks of stderr. * Fixed propagation of options to nested test suites.

Related projects

Advanced Bash-Scripting Guide
__ An in-depth exploration of the art of shell scripting by The Linux Documentation Project proposes a mechanism inspired by C, similar to


__ ShUnit is a testing framework of the xUnit family for Bourne derived shells. It is quite feature-rich but requires a whole lot of boilerplate to write a basic test suite. aims to be lightweight and easy to setup.


__ shUnit2 is a modern xUnit-style testing framework. It comes with a bunch of magic to remove unneccessary verbosity. It requires extra care when crafting test cases with many subprocess invocations as you have to fall back to shell features to fetch results. wraps this functionality out of the box.


__ A Test Anything Protocol (TAP) producer with an inherently natural-language- style API. Unfortunately it's only of draft quality and decouples the test runner from analysis, which does not allow for features such as


__ Another TAP producer with syntactic sugar. It depends on
environments (set -e) to run its tests such that "each line is an assertion of truth."

__ Helpers to fake binaries and bash builtins. It supports mocking features such as expecting a certain number of invocations and plays well with


We use cookies. If you continue to browse the site, you agree to the use of cookies. For more information on our use of cookies please see our Privacy Policy.