Bashunit
xUnit unit testing framework for Bash scripts
Install / Use
/learn @djui/BashunitREADME
bashunit
bashunit is a unit testing framework for Bash scripts based on xUnit principles.
This is similar to the ShUnit and its successor shUnit2.
Usage
Functions starting with 'test' will be automatically evaluated.
1. Write test cases
testEcho() {
assertEqual "$(echo foo)" "foo"
assertReturn "$(echo foo)" 0
}
2. Include this script at the end of your test script
source $(dirname $0)/bashunit.bash
# eof
3. Run test suite
$ ./test_example
testEcho:4:Passed
testEcho:5:Passed
Done. 2 passed. 0 failed. 0 skipped.
The return code is equal to the amount of failed testcases.
Options can be given to the test script:
$ bash ./bashunit.bash
Usage: <testscript> [options...]
Options:
-v, --verbose Print expected and provided values
-s, --summary Only print summary omitting individual test results
-q, --quiet Do not print anything to standard output
-l, --lineshow Show failing or skipped line after line number
-f, --failed Print only individual failed test results
-h, --help Show usage screen
Dependencies
- Bash (
BASH_LINENO) - Shell colours
API
-
assert($1)$1: ExpressionAssert that a given expression evaluates to true.
-
assertEqual($1, $2)$1: Output$2: ExpectedAssert that a given output string is equal to an expected string.
-
assertNotEqual($1, $2)$1: Output$2: ExpectedAssert that a given output string is not equal to an expected string.
-
assertStartsWith($1, $2)$1: Output$2: ExpectedAssert that a given output string starts with an expected string.
-
assertReturn($1, $2)$1: Output$2: Expected$?: ProvidedAssert that the last command's return code is equal to an expected integer.
-
assertNotReturn($1, $2)$1: Output$2: Expected$?: ProvidedAssert that the last command's return code is not equal to an expected integer.
-
assertGreaterThan($1, $2)$1Output$2ExpectedAssert that a given integer is greater than an expected integer.
-
assertAtLeast($1, $2)$1Output$2ExpectedAssert that a given integer is greater than or equal to an expected integer.
-
assertLessThan($1, $2)$1Output$2ExpectedAssert that a given integer is less than an expected integer.
-
assertAtMost($1, $2)$1Output$2ExpectedAssert that a given integer is less than or equal to an expected integer.
-
skip()Skip the current test case.
License
bashunit is licenced under a
BSD License.
Related Skills
node-connect
349.2kDiagnose OpenClaw node connection and pairing failures for Android, iOS, and macOS companion apps
frontend-design
109.5kCreate distinctive, production-grade frontend interfaces with high design quality. Use this skill when the user asks to build web components, pages, or applications. Generates creative, polished code that avoids generic AI aesthetics.
openai-whisper-api
349.2kTranscribe audio via OpenAI Audio Transcriptions API (Whisper).
qqbot-media
349.2kQQBot 富媒体收发能力。使用 <qqmedia> 标签,系统根据文件扩展名自动识别类型(图片/语音/视频/文件)。
