shunit21
Unit test framework for shell scripts
Usage
shunit2 unit-file
Overview
shUnit2 is an xUnit unit test framework for Bourne-based shell scripts, and is designed to work in a similar manner to JUnit, PyUnit, and so forth. If you have ever had the desire to write a unit test for a shell script, shUnit2 can do the job.
You can either run shunit2 directly from the command line and specify the unit file unit-file or directly source the shunit2 executable.
If you directly execute shunit2 and don't specify unit-file, shUnit2 assumes an empty test file and will return without an error message.
Options
shunit2 does not support any command-line options at all. You can either source shunit2 with the shell's . (“dot”) built-in to execute your unit tests or directly run shunit2 as a command with unit-file as a parameter.
Examples
The following simple script tests whether 1 equals 1.
#! /bin/sh
testEquality()
{
assertEquals 1 1
}
# load shunit2 . shunit2
The above example assumes this is executed on the machine where shunit2 is available as an executable command found in the path specified by the environment variable $PATH. You can alternatively copy a /usr/bin/shunit2 file from this machine to path/to/shunit2 in the target source tree and use it in the target build environment as follows.
#! /bin/sh
testEquality()
{
assertEquals 1 1
}
# load shunit2 . path/to/shunit2
The following is a shell session transcript showing how to write the same test as above and execute it directly.
$
cat > test_equality.sh <<EOF
testEquality()
{
assertEquals 1 1
}
EOF
$
shunit2 test_equality.sh
Note that shunit2 is not sourced in this example.
More sophisticated examples can be found in ⟨file:///usr/share/doc/shunit2/examples/⟩.
History
- Debian 13 (Trixie) to Debian 12 (Bookworm): the text shown here
- Debian 11 (Bullseye)
- Debian 10 (Buster) to Debian 9 (Stretch)
- Debian 8 (Jessie) to Debian 7 (Wheezy)
- Debian 6.0 (Squeeze)