A simple bash test runner
Explore the docs »
View Demo
·
Report Bug
·
Request Feature
To get a local copy up and running follow these simple steps.
All you need is a compatible shell eg bash
- Clone the shUnit repo
git clone https://github.com/F0xedb/shUnit.git
copy shunit and assert.sh to your path. eg
cp shunit /usr/bin/shunit
cp assert.sh /usr/bin/assert.sh
Arch user can do the following
yay -Syu shunit
# or manually
makepkg -si
Running shunit
is relatively simple cd
into the root directory of the project and run shunit
shunit will run all files ending with .shunit
Here is a basic unit test file
# unit test showcase
function Test_AequalsA {
AssertEqual "a" "a" # no problem
AssertContains "abcdefg" "def" # no problem
}
function Test_Fail {
AssertEquals "a" "b" # error a != b
}
The functions using assert should be prefixed with Test. Only these function will be ran by the unit test runner. Any other function (except for special functions) are helper functions you can use.
The runner outputs the following
$ shunit
> Running unit test : unit test showcase
> Pass: Test_AequalsA
> Error: Test_Fail failed - expected a got b
> unit test failed: Pass: 1 Error: 1
Here is a short list of assertions:
AssertEquals
input a, b -> must equal or an error occursAssertFileEquals
input a, b(files) -> must equal or an error occursAssertContains
input a,b -> b must be a substring of aAssertFileContains
input a,b (files- -> b must be inside of aAssertExists
input a -> Check if a file or variable existsAssertTrue
input a -> give a shell statement to check if something exists eg[ -f "$A" ]
Assert
input a -> Fails automatically when called. The input is a short message of what went wrong
Here are some simple examples
# Assertion examples
# special functions
# ran once on startup
function setup {
touch a
cat <<EOF > b
b
EOF
}
# runs after all tests are completed
function destroy {
rm a b
}
# unit tests
function Test_exists {
file="$HOME/h"
AssertExists "$file" # file doesn't exist so this test fails
}
function Test_content {
AssertFileEquals "a" "b" # the content is not the same so the test failes
}
function Test_equals {
AssertEquals "a" "a" # the strings are the same so the test passes
}
function Test_filecontains {
AssertFileContains "b" "a" # file b contains data in file a so a pass
AssertFileContains "a" "b" # file a contains nothing so it doesn't contain the data in b => test failed
}
function Test_assert {
if [[ "a" != "b" ]]; then
# The 1 indicates that the assert is an error
# 0 would indicate a success and generate a Pass: check
assert "1" "A does not equal b" # just errors when this is called
fi
}
function Test_assert_true {
assertTrue [[ "a" != "b" ]] # this is the same as the assert function
}
Optionally we have "special" functions for helping your unit tests
setup
# called once per file at the startdestory
# called once per file at the end (even when a test fails)setupTest
# called before each functiondestroyTest
# called after each function
These function don't need to be used, but are usefull to clean up the test suite For more examples, please refer to the Documentation
See the open issues for a list of proposed features (and known issues).
Contributions are what make the open source community such an amazing place to be learn, inspire, and create. Any contributions you make are greatly appreciated.
- Fork the Project
- Create your Feature Branch (
git checkout -b feature/AmazingFeature
) - Commit your Changes (
git commit -m 'Add some AmazingFeature'
) - Push to the Branch (
git push origin feature/AmazingFeature
) - Open a Pull Request
Distributed under the GPL License. See LICENSE
for more information.
F0xedb - [email protected]
Project Link: https://github.com/F0xedb/shUnit