Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rename assert skipping functions and add possibility to skip a test completely #161

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
90 changes: 72 additions & 18 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -369,21 +369,41 @@ useful to clean up the environment after each test.

### <a name="skipping"></a> Skipping

startSkipping
To "skip" a test completely, call function

This function forces the remaining _assert_ and _fail_ functions to be
"skipped", i.e. they will have no effect. Each function skipped will be recorded
skipTest

in the very beginning of the test case. It **must** be called as first thing in
a test that one wishes to skip. The function will force all _assert_ and _fail_
functions to be ignored completely in that test. The number of skipped asserts
and fails *will not* be recorded, instead a skipped test counter is increased by one.
Should this function be called after any asserts, an error will be printed out
and the test will fail.

To "skip" only certain assert and fail functions, one should call function

startSkippingAsserts

This function forces the _assert_ and _fail_ functions after the function call to be
"skipped", i.e. they will have no effect. Each skipped check function will be recorded
so that the total of asserts and fails will not be altered.

endSkipping
To end assert and fail function skipping, call function

endSkippingAsserts

This function returns calls to the _assert_ and _fail_ functions to their
default behavior, i.e. they will be called.

isSkipping
The current state of assert skipping can be checked using function

isSkippingAsserts

It can be compared against `${SHUNIT_TRUE}` or `${SHUNIT_FALSE}` if desired.

This function returns the current state of skipping. It can be compared against
`${SHUNIT_TRUE}` or `${SHUNIT_FALSE}` if desired.
**Note:** Those `*SkippingAsserts` functions were previously called `startSkipping`,
`endSkipping`, and `isSkipping`, respectively. The old functions are still available
and they work as before, but they will print out a warning that they are deprecated.

### <a name="suites"></a> Suites

Expand Down Expand Up @@ -496,8 +516,8 @@ but maintain the total test count.

Probably the easiest example would be shell code that is meant to run under the
__bash__ shell, but the unit test is running under the Bourne shell. There are
things that just won't work. The following test code demonstrates two sample
functions, one that will be run under any shell, and the another that will run
things that just won't work. The following test code demonstrates three sample
functions, one that will be run under any shell, and the two others that will run
only under the __bash__ shell.

_**Example** -- math include_
Expand All @@ -517,9 +537,19 @@ add_bash() {

echo $(($1 + $2))
}

subtract_bash()
{
num_a=$1
num_b=$2

echo $(($1 - $2))
}
```

And here is a corresponding unit test that correctly skips the `add_bash()` function when the unit test is not running under the __bash__ shell.
And here is corresponding unit tests that correctly skips the `add_bash()`
and `subtract_bash()` functions when the unit test is not running under
the __bash__ shell.

_**Example** -- math unit test_
```sh
Expand All @@ -533,12 +563,27 @@ testAdding() {
3 "${result}"

# Disable non-generic tests.
[ -z "${BASH_VERSION:-}" ] && startSkipping
[ -z "${BASH_VERSION:-}" ] && startSkippingAsserts

result=`add_bash 1 2`
assertEquals \
"the result of '${result}' was wrong" \
3 "${result}"
}

testBashFunctions() {
# Disable non-generic tests.
[ -z "${BASH_VERSION:-}" ] && skipTest "Works only in bash shell"

result=`add_bash 1 2`
assertEquals \
"the result of '${result}' was wrong" \
3 "${result}"

result=`subtract_bash 2 1`
assertEquals \
"the result of '${result}' was wrong" \
1 "${result}"
}

oneTimeSetUp() {
Expand All @@ -556,8 +601,9 @@ output.
```console
$ /bin/bash math_test.sh
testAdding
testBashFunctions

Ran 1 test.
Ran 2 tests.

OK
```
Expand All @@ -568,20 +614,28 @@ output.
```console
$ /bin/ksh math_test.sh
testAdding
testBashFunctions
shunit2:WARN [skipping test] Works only in bash shell

Ran 1 test.
Ran 2 tests.

OK (skipped=1)
OK (skipped tests=1,skipped asserts=1)
```

As you can see, the total number of tests has not changed, but the report
indicates that some tests were skipped.
indicates that some tests and asserts were skipped. Also, a warning is printed out to stderr
when running the test in a shell other than bash for the bash-only test case to indicate that
the test is skipped.

Skipping can be controlled with the following functions: `startSkipping()`,
`endSkipping()`, and `isSkipping()`. Once skipping is enabled, it will remain
enabled until the end of the current test function call, after which skipping is
Skipping assert checks can be controlled with the following functions: `startSkippingAsserts()`,
`endSkippingAsserts()`, and `isSkippingAsserts()`. Once skipping is enabled, it will remain
enabled until the end of the current test function call, after which assert skipping is
disabled.

Should the test be skipped completely, it can be done using the `skipTest` function. It will
affect only the test case in question, and it does not increment the counters for the asserts
(total, passed, failed, or skipped).

### <a name="cmd-line-args"></a> Running specific tests from the command line.

When running a test script, you may override the default set of tests, or the suite-specified set of tests, by providing additional arguments on the command line. Each additional argument after the `--` marker is assumed to be the name of a test function to be run in the order specified. e.g.
Expand Down
8 changes: 8 additions & 0 deletions examples/math.inc
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,11 @@ add_bash()

echo $(($1 + $2))
}

subtract_bash()
{
num_a=$1
num_b=$2

echo $(($1 - $2))
}
17 changes: 16 additions & 1 deletion examples/math_test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,29 @@ testAdding() {
3 "${result}"

# Disable non-generic tests.
[ -z "${BASH_VERSION:-}" ] && startSkipping
[ -z "${BASH_VERSION:-}" ] && startSkippingAsserts

result=`add_bash 1 2`
assertEquals \
"the result of '${result}' was wrong" \
3 "${result}"
}

testBashFunctions() {
# Disable non-generic tests.
[ -z "${BASH_VERSION:-}" ] && skipTest "Works only in bash shell"

result=`add_bash 1 2`
assertEquals \
"the result of '${result}' was wrong" \
3 "${result}"

result=`subtract_bash 2 1`
assertEquals \
"the result of '${result}' was wrong" \
1 "${result}"
}

oneTimeSetUp() {
# Load include to test.
. ./math.inc
Expand Down
Loading