assert.sh is test-driven development in the Bourne again shell.
Version: 1.1 Author: Robert Lehmann License: LGPLv3. assert.sh # `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 `true` is expected to be 0 assert_raises "true" # exit code of `false` is expected to be 1 assert_raises "false" 1 # end of test suite assert_end examples
If you had written the above snippet into tests.sh
you could invoke it without any extra hassle:
$ ./tests.sh all 4 examples tests passed in 0.014s.
Watch out to have tests.sh
executable (chmod +x tests.sh
), otherwise you need to invoke it with bash tests.sh
.
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 assert_end
(or write another assert_end
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 tests.sh ... $ echo $? 1
assert
and assert_raises
onlyassert.sh
and you're doneYou 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 stdin
, command line arguments argv
, 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):
wget https://raw.github.com/lehmannro/assert.sh/v1.1/assert.sh
Use the following command to grab a snapshot of the current development version:
wget https://raw.github.com/lehmannro/assert.sh/master/assert.sh
There is no additional build/compile step except for changing permissions (chmod +x
) depending on the way you have chosen to install assert.sh.
The bpkg
package manager allows you to install assert.sh locally:
bpkg install lehmannro/assert.sh
(Watch out to source deps/assert/assert.sh
instead.)
If you want to install globally, for your whole system, use:
bpkg install lehmannro/assert.sh -g
assert <command> [stdout] [stdin]
Check for an expected output when running your command. stdout supports all control sequences echo -e
interprets, eg. \n
for a newline. The default stdout is assumed to be empty.
assert_raises <command> [exitcode] [stdin]
Verify command terminated with the expected status code. The default exitcode 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 <command>
Skip the following test case if command exits successfully. (skip
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.
See assert.sh --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.variable corresponding option
$DEBUG
--verbose
$STOP
--stop
$INVARIANT
--invariant
$DISCOVERONLY
--discover-only
$CONTINUE
--continue
skip
and skip_if
commands.set -e
environments (closes #6, thanks David Schoen.)--continue
flag to avoid tinkering with the exit code.bc
dependency (closes #8, thanks Maciej Żok.)set -u
environments (closes #1.)--collect-only
and --stop
.
errexit
environments (set -e) to run its tests such that "each line is an assertion of truth."
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4