-
Notifications
You must be signed in to change notification settings - Fork 3
Bash: Unit Testing
Without a doubt, we discovered writing bash functions was simple - testing them for "the long haul" much more challenging. Our "knee-jerk" reaction was to write "one off" tests - but that doesn't ensure our quality should we need to make changes. With that said, we've provide a test harness vary similar in nature to JUnit in what we call the test-utils.sh framework. If you are familiar with JUnit this should "hopefully" be obvious. We do unit test (or try to) all our functions as can be found here.
Asserts allow one to test certain conditions and fail if those conditions are not met (as in exit with a 1
as the status code). Assert function names should resemble the intent.
This function accepts two parameters and ensures those parameters are equal.
# Will fail (exit code 1)
assert-equals "foo bar" "alpha beta"
# Will pass (exit code 0)
assert-equals "theta force" "theta force"
# Pass/fail depends on the values of ${foo} and ${bar}
assert-equals "${foo}" "${bar}"
This function accepts two parameters and ensures those parameters are not equal.
# Will pass (exit code 0)
assert-not-equals "foo bar" "alpha beta"
# Will fail (exit code 1)
assert-not-equals "theta force" "theta force"
# Pass/fail depends on the values of ${foo} and ${bar}
assert-not-equals "${foo}" "${bar}"
This function accepts 1 parameter and ensures it is blank (empty string).
# Will pass (exit code 0)
assert-blank ""
# Will fail (exit code 1)
assert-blank " "
# Pass/fail depends on the values of ${foo}
assert-blank "${foo}"
This function accepts 1 parameter and ensures it is not blank (empty string).
# Will pass (exit code 0)
assert-not-blank ""
# Will fail (exit code 1)
assert-not-blank " "
assert-not-blank "Hello world"
# Pass/fail depends on the values of ${foo}
assert-blank "${foo}"
#### assert-file-exists
This function accepts 1 parameter and ensures a file exists. If the parameter is blank, it will fail.
#### Example
```bash
# Will pass (exit code 0)
assert-not-blank ""
# Will fail (exit code 1)
assert-not-blank " "
assert-not-blank "Hello world"
# Pass/fail depends on the values of ${foo}
assert-blank "${foo}"
## Covered Code
The following unit tests have high test coverage:
* [common-utils.sh](https://github.com/FlossWare/scripts/blob/master/bash/common-utils.sh)
* [gitrepo-utils.sh](https://github.com/FlossWare/scripts/blob/master/bash/guthub-utils.sh)
* [git-utils.sh](https://github.com/FlossWare/scripts/blob/master/bash/git-utils.sh)
* [jenkins-utils.sh](https://github.com/FlossWare/scripts/blob/master/bash/jenkins-utils.sh)
* [json-utils.sh](https://github.com/FlossWare/scripts/blob/master/bash/json-utils.sh)
* [maven-utils.sh](https://github.com/FlossWare/scripts/blob/master/bash/maven-utils.sh)
* [rpm-utils.sh](https://github.com/FlossWare/scripts/blob/master/bash/rpm-utils.sh)