Sharness is a portable shell library to write, run, and analyze automated tests for Unix programs. Since all tests output TAP, the Test Anything Protocol, they can be run with any TAP harness.
Each test is written as a shell script, for example:
#!/bin/sh
test_description="Show basic features of Sharness"
. ./sharness.sh
test_expect_success "Success is reported like this" "
echo hello world | grep hello
"
test_expect_success "Commands are chained this way" "
test x = 'x' &&
test 2 -gt 1 &&
echo success
"
return_42() {
echo "Will return soon"
return 42
}
test_expect_success "You can test for a specific exit code" "
test_expect_code 42 return_42
"
test_expect_failure "We expect this to fail" "
test 1 = 2
"
test_done
Running the above test script returns the following (TAP) output:
$ ./simple.t
ok 1 - Success is reported like this
ok 2 - Commands are chained this way
ok 3 - You can test for a specific exit code
not ok 4 - We expect this to fail # TODO known breakage
# still have 1 known breakage(s)
# passed all remaining 3 test(s)
1..4
Alternatively, you can run the test through prove(1):
$ prove simple.t
simple.t .. ok
All tests successful.
Files=1, Tests=4, 0 wallclock secs ( 0.02 usr + 0.00 sys = 0.02 CPU)
Result: PASS
Sharness was derived from the Git project - see README.git for the original documentation.
First, clone the Git repository:
$ git clone git://github.com/mlafeldt/sharness.git
Then choose an installation method that works best for you:
If you like to add Sharness to the sources of a project you want to use it for,
simply copy the files sharness.sh
, aggregate-results.sh
, and test/Makefile
to a folder named test
inside that project.
Alternatively, you can also add Sharness as a Git submodule to your project.
$ cd sharness
$ make install
This will install Sharness to $HOME/share/sharness
, and its documentation and
examples to $HOME/share/doc/sharness
.
$ cd sharness
# make install prefix=/usr/local
This will install Sharness to /usr/local/share/sharness
, and its documentation
and examples to /usr/local/share/doc/sharness
.
Of course, you can change the prefix parameter to install Sharness to any other location.
If you want to install Sharness with Opscode Chef, the Sharness cookbook is for you.
The following files are essential to using Sharness:
sharness.sh
- core shell library providing test functionality, see separate API documentationaggregate-results.sh
- helper script to aggregate test resultstest/Makefile
- test driver
To learn how to write and run actual test scripts based on sharness.sh
, please
read README.git until I come up with more documentation myself.
See how Sharness is used in real-world projects:
Furthermore, as Sharness was derived from Git, Git's test suite is worth examining as well, especially if you're interested in managing a big number of tests.
Here is a list of other shell testing libraries (sorted alphabetically):
Sharness is licensed under the terms of the GNU General Public License version 2 or higher. See file COPYING for full license text.
- Web: http://mlafeldt.github.com/sharness
- Mail: [email protected]
- Twitter: @mlafeldt