Skip to content
This repository was archived by the owner on Jun 21, 2020. It is now read-only.

Bash: Unit Testing

Scot P. Floess edited this page Oct 14, 2017 · 22 revisions

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.

How To

Asserts

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.

assert-equals

This function accepts two parameters and ensures those parameters are equal.

Example

# 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}"

assert-not-equals

This function accepts two parameters and ensures those parameters are not equal.

Example

# 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}"

assert-blank

This function accepts 1 parameter and ensures it is blank (empty string).

Example

# 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}"

assert-not-blank

This function accepts 1 parameter and ensures it is not blank (empty string).

Example

# 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)
Clone this wiki locally