From 5040d9cd7dcfe17afa466438181203f805f7df47 Mon Sep 17 00:00:00 2001 From: Jussi Pekonen Date: Tue, 5 Jul 2022 14:02:43 +0300 Subject: [PATCH 1/2] Rename *Skipping functions to *SkippingAsserts to indicate that they are related to assert skipping --- README.md | 30 ++++++++++++++++++++---------- examples/math_test.sh | 2 +- shunit2 | 37 +++++++++++++++++++++++++------------ shunit2_general_test.sh | 32 ++++++++++++++++---------------- shunit2_macros_test.sh | 24 ++++++++++++------------ shunit2_misc_test.sh | 2 +- shunit2_shopt_test.sh | 2 +- shunit2_test_helpers | 2 +- 8 files changed, 77 insertions(+), 54 deletions(-) diff --git a/README.md b/README.md index 42b66d2..0614db3 100644 --- a/README.md +++ b/README.md @@ -369,22 +369,32 @@ useful to clean up the environment after each test. ### Skipping - startSkipping +#### Skipping tests + +TBA + +#### Skipping assertions + + startSkippingAsserts 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 so that the total of asserts and fails will not be altered. - endSkipping + endSkippingAsserts This function returns calls to the _assert_ and _fail_ functions to their default behavior, i.e. they will be called. - isSkipping + isSkippingAsserts -This function returns the current state of skipping. It can be compared against +This function returns the current state of assert skipping. It can be compared against `${SHUNIT_TRUE}` or `${SHUNIT_FALSE}` if desired. +**Note:** These functions were called `startSkipping`, `endSkipping`, and `isSkipping` +earlier. Those old functions are still available and they work as before, but they will +print out a warning that they are deprecated. + ### Suites The default behavior of shUnit2 is that all tests will be found dynamically. If @@ -533,7 +543,7 @@ testAdding() { 3 "${result}" # Disable non-generic tests. - [ -z "${BASH_VERSION:-}" ] && startSkipping + [ -z "${BASH_VERSION:-}" ] && startSkippingAsserts result=`add_bash 1 2` assertEquals \ @@ -571,15 +581,15 @@ testAdding Ran 1 test. -OK (skipped=1) +OK (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 asserts were 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. ### Running specific tests from the command line. diff --git a/examples/math_test.sh b/examples/math_test.sh index c6d0029..c668df5 100755 --- a/examples/math_test.sh +++ b/examples/math_test.sh @@ -8,7 +8,7 @@ testAdding() { 3 "${result}" # Disable non-generic tests. - [ -z "${BASH_VERSION:-}" ] && startSkipping + [ -z "${BASH_VERSION:-}" ] && startSkippingAsserts result=`add_bash 1 2` assertEquals \ diff --git a/shunit2 b/shunit2 index 57a45da..492b7ad 100755 --- a/shunit2 +++ b/shunit2 @@ -78,7 +78,7 @@ __shunit_lineno='' # Line number of executed test. __shunit_mode=${__SHUNIT_MODE_SOURCED} # Operating mode. __shunit_reportGenerated=${SHUNIT_FALSE} # Is report generated. __shunit_script='' # Filename of unittest script (standalone mode). -__shunit_skip=${SHUNIT_FALSE} # Is skipping enabled. +__shunit_skip_asserts=${SHUNIT_FALSE} # Is assert skipping enabled. __shunit_suite='' # Suite of tests to execute. __shunit_clean=${SHUNIT_FALSE} # _shunit_cleanup() was already called. @@ -827,21 +827,29 @@ _FAIL_NOT_SAME_='eval failNotSame --lineno "${LINENO:-}"' # Force remaining assert and fail functions to be "skipped". # # 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 so that -# the total of asserts and fails will not be altered. +# i.e. they will have no effect. Each skipped assert and fail function will be +# recorded so that the total of asserts and fails will not be altered. # # Args: # message: string: message to provide to user [optional] +startSkippingAsserts() { + if ${__SHUNIT_BUILTIN} [ $# -gt 0 ]; then _shunit_warn "[skipping asserts] $*"; fi + __shunit_skip_asserts=${SHUNIT_TRUE} +} startSkipping() { - if ${__SHUNIT_BUILTIN} [ $# -gt 0 ]; then _shunit_warn "[skipping] $*"; fi - __shunit_skip=${SHUNIT_TRUE} + _shunit_warn "startSkipping is deprecated. Use startSkippingAsserts instead." + startSkippingAsserts "$@" } # Resume the normal recording behavior of assert and fail calls. # # Args: # None -endSkipping() { __shunit_skip=${SHUNIT_FALSE}; } +endSkippingAsserts() { __shunit_skip_asserts=${SHUNIT_FALSE}; } +endSkipping() { + _shunit_warn "endSkipping is deprecated. Use endSkippingAsserts instead." + endSkippingAsserts +} # Returns the state of assert and fail call skipping. # @@ -849,7 +857,12 @@ endSkipping() { __shunit_skip=${SHUNIT_FALSE}; } # None # Returns: # boolean: (TRUE/FALSE constant) -isSkipping() { return ${__shunit_skip}; } +isSkippingAsserts() { return ${__shunit_skip_asserts}; } +isSkipping() { + _shunit_warn "isSkipping is deprecated. Use isSkippingAsserts instead." + # shellcheck disable=SC2046 + return `isSkippingAsserts` +} #----------------------------------------------------------------------------- # Suite functions. @@ -1099,8 +1112,8 @@ _shunit_execSuite() { for _shunit_test_ in ${__shunit_suite}; do __shunit_testSuccess=${SHUNIT_TRUE} - # Disable skipping. - endSkipping + # Disable skipping asserts. + endSkippingAsserts # Execute the per-test setUp() function. if ! setUp; then @@ -1160,13 +1173,13 @@ _shunit_generateReport() { if ${__SHUNIT_BUILTIN} [ ${_shunit_ok_} -eq ${SHUNIT_TRUE} ]; then _shunit_msg_="${__shunit_ansi_green}OK${__shunit_ansi_none}" if ${__SHUNIT_BUILTIN} [ "${__shunit_assertsSkipped}" -gt 0 ]; then - _shunit_msg_="${_shunit_msg_} (${__shunit_ansi_yellow}skipped=${__shunit_assertsSkipped}${__shunit_ansi_none})" + _shunit_msg_="${_shunit_msg_} (${__shunit_ansi_yellow}skipped asserts=${__shunit_assertsSkipped}${__shunit_ansi_none})" fi else _shunit_msg_="${__shunit_ansi_red}FAILED${__shunit_ansi_none}" _shunit_msg_="${_shunit_msg_} (${__shunit_ansi_red}failures=${__shunit_assertsFailed}${__shunit_ansi_none}" if ${__SHUNIT_BUILTIN} [ "${__shunit_assertsSkipped}" -gt 0 ]; then - _shunit_msg_="${_shunit_msg_},${__shunit_ansi_yellow}skipped=${__shunit_assertsSkipped}${__shunit_ansi_none}" + _shunit_msg_="${_shunit_msg_},${__shunit_ansi_yellow}skipped asserts=${__shunit_assertsSkipped}${__shunit_ansi_none}" fi _shunit_msg_="${_shunit_msg_})" fi @@ -1185,7 +1198,7 @@ _shunit_generateReport() { # Returns: # boolean: whether the test should be skipped (TRUE/FALSE constant) _shunit_shouldSkip() { - if ${__SHUNIT_BUILTIN} test ${__shunit_skip} -eq ${SHUNIT_FALSE}; then + if ${__SHUNIT_BUILTIN} test ${__shunit_skip_asserts} -eq ${SHUNIT_FALSE}; then return ${SHUNIT_FALSE} fi _shunit_assertSkip diff --git a/shunit2_general_test.sh b/shunit2_general_test.sh index c7e13a4..0415d56 100755 --- a/shunit2_general_test.sh +++ b/shunit2_general_test.sh @@ -20,57 +20,57 @@ stderrF="${TMPDIR:-/tmp}/STDERR" # Load test helpers. . ./shunit2_test_helpers -testSkipping() { - # We shouldn't be skipping to start. - if isSkipping; then - th_error 'skipping *should not be* enabled' +testAssertsSkipping() { + # We shouldn't be assert skipping to start. + if isSkippingAsserts; then + th_error 'assert skipping *should not be* enabled' return fi - startSkipping + startSkippingAsserts was_skipping_started=${SHUNIT_FALSE} - if isSkipping; then was_skipping_started=${SHUNIT_TRUE}; fi + if isSkippingAsserts; then was_skipping_started=${SHUNIT_TRUE}; fi - endSkipping + endSkippingAsserts was_skipping_ended=${SHUNIT_FALSE} - if isSkipping; then was_skipping_ended=${SHUNIT_TRUE}; fi + if isSkippingAsserts; then was_skipping_ended=${SHUNIT_TRUE}; fi - assertEquals "skipping wasn't started" "${was_skipping_started}" "${SHUNIT_TRUE}" - assertNotEquals "skipping wasn't ended" "${was_skipping_ended}" "${SHUNIT_TRUE}" + assertEquals "assert skipping wasn't started" "${was_skipping_started}" "${SHUNIT_TRUE}" + assertNotEquals "assert skipping wasn't ended" "${was_skipping_ended}" "${SHUNIT_TRUE}" return 0 } -testStartSkippingWithMessage() { +testStartSkippingAssertsWithMessage() { unittestF="${SHUNIT_TMPDIR}/unittest" sed 's/^#//' >"${unittestF}" <<\EOF ## Start skipping with a message. #testSkipping() { -# startSkipping 'SKIP-a-Dee-Doo-Dah' +# startSkippingAsserts 'SKIP-a-Dee-Doo-Dah' #} #SHUNIT_COLOR='none' #. ${TH_SHUNIT} EOF # Ignoring errors with `|| :` as we only care about `FAILED` in the output. ( exec "${SHELL:-sh}" "${unittestF}" >"${stdoutF}" 2>"${stderrF}" ) || : - if ! grep '\[skipping\] SKIP-a-Dee-Doo-Dah' "${stderrF}" >/dev/null; then + if ! grep '\[skipping asserts\] SKIP-a-Dee-Doo-Dah' "${stderrF}" >/dev/null; then fail 'skipping message was not generated' fi return 0 } -testStartSkippingWithoutMessage() { +testStartSkippingAssertsWithoutMessage() { unittestF="${SHUNIT_TMPDIR}/unittest" sed 's/^#//' >"${unittestF}" <<\EOF ## Start skipping with a message. #testSkipping() { -# startSkipping +# startSkippingAsserts #} #SHUNIT_COLOR='none' #. ${TH_SHUNIT} EOF # Ignoring errors with `|| :` as we only care about `FAILED` in the output. ( exec "${SHELL:-sh}" "${unittestF}" >"${stdoutF}" 2>"${stderrF}" ) || : - if grep '\[skipping\]' "${stderrF}" >/dev/null; then + if grep '\[skipping asserts\]' "${stderrF}" >/dev/null; then fail 'skipping message was unexpectedly generated' fi return 0 diff --git a/shunit2_macros_test.sh b/shunit2_macros_test.sh index a6d7e1e..daf7ba1 100755 --- a/shunit2_macros_test.sh +++ b/shunit2_macros_test.sh @@ -21,7 +21,7 @@ stderrF="${TMPDIR:-/tmp}/STDERR" . ./shunit2_test_helpers testAssertEquals() { - isLinenoWorking || startSkipping + isLinenoWorking || startSkippingAsserts ( ${_ASSERT_EQUALS_} 'x' 'y' >"${stdoutF}" 2>"${stderrF}" ) if ! wasAssertGenerated; then @@ -37,7 +37,7 @@ testAssertEquals() { } testAssertNotEquals() { - isLinenoWorking || startSkipping + isLinenoWorking || startSkippingAsserts ( ${_ASSERT_NOT_EQUALS_} 'x' 'x' >"${stdoutF}" 2>"${stderrF}" ) if ! wasAssertGenerated; then @@ -53,7 +53,7 @@ testAssertNotEquals() { } testSame() { - isLinenoWorking || startSkipping + isLinenoWorking || startSkippingAsserts ( ${_ASSERT_SAME_} 'x' 'y' >"${stdoutF}" 2>"${stderrF}" ) if ! wasAssertGenerated; then @@ -69,7 +69,7 @@ testSame() { } testNotSame() { - isLinenoWorking || startSkipping + isLinenoWorking || startSkippingAsserts ( ${_ASSERT_NOT_SAME_} 'x' 'x' >"${stdoutF}" 2>"${stderrF}" ) if ! wasAssertGenerated; then @@ -85,7 +85,7 @@ testNotSame() { } testNull() { - isLinenoWorking || startSkipping + isLinenoWorking || startSkippingAsserts ( ${_ASSERT_NULL_} 'x' >"${stdoutF}" 2>"${stderrF}" ) if ! wasAssertGenerated; then @@ -101,7 +101,7 @@ testNull() { } testNotNull() { - isLinenoWorking || startSkipping + isLinenoWorking || startSkippingAsserts ( ${_ASSERT_NOT_NULL_} '' >"${stdoutF}" 2>"${stderrF}" ) if ! wasAssertGenerated; then @@ -117,7 +117,7 @@ testNotNull() { } testAssertTrue() { - isLinenoWorking || startSkipping + isLinenoWorking || startSkippingAsserts ( ${_ASSERT_TRUE_} "${SHUNIT_FALSE}" >"${stdoutF}" 2>"${stderrF}" ) if ! wasAssertGenerated; then @@ -133,7 +133,7 @@ testAssertTrue() { } testAssertFalse() { - isLinenoWorking || startSkipping + isLinenoWorking || startSkippingAsserts ( ${_ASSERT_FALSE_} "${SHUNIT_TRUE}" >"${stdoutF}" 2>"${stderrF}" ) if ! wasAssertGenerated; then @@ -149,7 +149,7 @@ testAssertFalse() { } testFail() { - isLinenoWorking || startSkipping + isLinenoWorking || startSkippingAsserts ( ${_FAIL_} >"${stdoutF}" 2>"${stderrF}" ) if ! wasAssertGenerated; then @@ -165,7 +165,7 @@ testFail() { } testFailNotEquals() { - isLinenoWorking || startSkipping + isLinenoWorking || startSkippingAsserts ( ${_FAIL_NOT_EQUALS_} 'x' 'y' >"${stdoutF}" 2>"${stderrF}" ) if ! wasAssertGenerated; then @@ -181,7 +181,7 @@ testFailNotEquals() { } testFailSame() { - isLinenoWorking || startSkipping + isLinenoWorking || startSkippingAsserts ( ${_FAIL_SAME_} 'x' 'x' >"${stdoutF}" 2>"${stderrF}" ) if ! wasAssertGenerated; then @@ -197,7 +197,7 @@ testFailSame() { } testFailNotSame() { - isLinenoWorking || startSkipping + isLinenoWorking || startSkippingAsserts ( ${_FAIL_NOT_SAME_} 'x' 'y' >"${stdoutF}" 2>"${stderrF}" ) if ! wasAssertGenerated; then diff --git a/shunit2_misc_test.sh b/shunit2_misc_test.sh index 12cc2d4..11f80e6 100755 --- a/shunit2_misc_test.sh +++ b/shunit2_misc_test.sh @@ -172,7 +172,7 @@ EOF testIssue123() { if [ -z "${BASH_SUBSHELL:-}" ]; then # shellcheck disable=SC2016 - startSkipping 'The ${BASH_SUBSHELL} variable is unavailable in this shell.' + startSkippingAsserts 'The ${BASH_SUBSHELL} variable is unavailable in this shell.' fi # shellcheck disable=SC2016 assertTrue 'not in subshell' '[[ ${BASH_SUBSHELL} -eq 0 ]]' diff --git a/shunit2_shopt_test.sh b/shunit2_shopt_test.sh index b373f5b..3cbf6ed 100755 --- a/shunit2_shopt_test.sh +++ b/shunit2_shopt_test.sh @@ -20,7 +20,7 @@ SHOPT_CMD='shopt' testNullglob() { - isShoptWorking || startSkipping + isShoptWorking || startSkippingAsserts nullglob=$(${SHOPT_CMD} nullglob |cut -f2) diff --git a/shunit2_test_helpers b/shunit2_test_helpers index 1add9b8..a993066 100644 --- a/shunit2_test_helpers +++ b/shunit2_test_helpers @@ -207,7 +207,7 @@ th_queryReturn() { # Providing external and internal calls to the showOutput helper function. th_showOutput() { _th_showOutput "$@"; } _th_showOutput() { - if isSkipping; then + if isSkippingAsserts; then return fi From 07141555a3a7cc7abe889ab22654a7acced0723a Mon Sep 17 00:00:00 2001 From: Jussi Pekonen Date: Thu, 7 Jul 2022 10:21:33 +0300 Subject: [PATCH 2/2] Add a possibility to skip a test completely This new functionality will disable the assert and fail calls for the test case and it will not increment the asserts counters (total, passed, failed, and skipped). --- README.md | 78 ++++++++++++++++++------ examples/math.inc | 8 +++ examples/math_test.sh | 15 +++++ shunit2 | 128 ++++++++++++++++++++++++++++++++++------ shunit2_general_test.sh | 72 +++++++++++++++++++++- 5 files changed, 263 insertions(+), 38 deletions(-) diff --git a/README.md b/README.md index 0614db3..64f29db 100644 --- a/README.md +++ b/README.md @@ -369,31 +369,41 @@ useful to clean up the environment after each test. ### Skipping -#### Skipping tests +To "skip" a test completely, call function -TBA + skipTest -#### Skipping assertions +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 remaining _assert_ and _fail_ functions to be -"skipped", i.e. they will have no effect. Each function skipped will be recorded +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. +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. +The current state of assert skipping can be checked using function + isSkippingAsserts -This function returns the current state of assert skipping. It can be compared against -`${SHUNIT_TRUE}` or `${SHUNIT_FALSE}` if desired. +It can be compared against `${SHUNIT_TRUE}` or `${SHUNIT_FALSE}` if desired. -**Note:** These functions were called `startSkipping`, `endSkipping`, and `isSkipping` -earlier. Those old functions are still available and they work as before, but they will -print out a warning that they are deprecated. +**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. ### Suites @@ -506,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_ @@ -527,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 @@ -551,6 +571,21 @@ testAdding() { 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 @@ -566,8 +601,9 @@ output. ```console $ /bin/bash math_test.sh testAdding +testBashFunctions -Ran 1 test. +Ran 2 tests. OK ``` @@ -578,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 asserts=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 asserts 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 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). + ### 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. diff --git a/examples/math.inc b/examples/math.inc index 4097106..7e09004 100644 --- a/examples/math.inc +++ b/examples/math.inc @@ -15,3 +15,11 @@ add_bash() echo $(($1 + $2)) } + +subtract_bash() +{ + num_a=$1 + num_b=$2 + + echo $(($1 - $2)) +} \ No newline at end of file diff --git a/examples/math_test.sh b/examples/math_test.sh index c668df5..411441b 100755 --- a/examples/math_test.sh +++ b/examples/math_test.sh @@ -16,6 +16,21 @@ testAdding() { 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 diff --git a/shunit2 b/shunit2 index 492b7ad..1cac4ec 100755 --- a/shunit2 +++ b/shunit2 @@ -78,6 +78,7 @@ __shunit_lineno='' # Line number of executed test. __shunit_mode=${__SHUNIT_MODE_SOURCED} # Operating mode. __shunit_reportGenerated=${SHUNIT_FALSE} # Is report generated. __shunit_script='' # Filename of unittest script (standalone mode). +__shunit_skip_test=${SHUNIT_FALSE} # Is test skipping enabled __shunit_skip_asserts=${SHUNIT_FALSE} # Is assert skipping enabled. __shunit_suite='' # Suite of tests to execute. __shunit_clean=${SHUNIT_FALSE} # _shunit_cleanup() was already called. @@ -93,12 +94,17 @@ __shunit_ansi_cyan='' __shunit_testSuccess=${SHUNIT_TRUE} __shunit_testsTotal=0 __shunit_testsPassed=0 +__shunit_testsSkipped=0 __shunit_testsFailed=0 # Counts of asserts. +__shunit_assertsTotalInTest=0 __shunit_assertsTotal=0 +__shunit_assertsPassedInTest=0 __shunit_assertsPassed=0 +__shunit_assertsFailedInTest=0 __shunit_assertsFailed=0 +__shunit_assertsSkippedInTest=0 __shunit_assertsSkipped=0 # @@ -824,6 +830,41 @@ _FAIL_NOT_SAME_='eval failNotSame --lineno "${LINENO:-}"' # Skipping functions. # +# Force the following test functions to be "skipped". +# +# This function will "mark" the following test cases to be "skipped", i.e. +# they will not be executed. Instead, they are indicated as skipped tests +# and the number of the skipped tests will be recorded in the final report. +skipTest() { + # The assert skipping must be disabled when calling this function + if isSkippingAsserts; then + endSkippingAsserts + _shunit_error "skipTest function MUST be called as the first thing in the test function!" + return ${SHUNIT_FALSE} + fi + # The no assert calls should be made before calling this function + _shunit_skip_test_counter=${__shunit_assertsTotalInTest} + _shunit_skip_test_counter=`expr "${_shunit_skip_test_counter}" + "${__shunit_assertsPassedInTest}"` + _shunit_skip_test_counter=`expr "${_shunit_skip_test_counter}" + "${__shunit_assertsFailedInTest}"` + _shunit_skip_test_counter=`expr "${_shunit_skip_test_counter}" + "${__shunit_assertsSkippedInTest}"` + if ${__SHUNIT_BUILTIN} [ "${_shunit_skip_test_counter}" -gt 0 ]; then + _shunit_error "skipTest function MUST be called as the first thing in the test function!" + return ${SHUNIT_FALSE} + fi + unset _shunit_skip_test_counter + __shunit_skip_test=${SHUNIT_TRUE} + _shunit_skip_test_message="[skipping test] $*" + if ${__SHUNIT_BUILTIN} [ $# -eq 0 ]; then _shunit_skip_test_message="[skipping test]"; fi + _shunit_warn "${_shunit_skip_test_message}" + unset _shunit_skip_test_message + return ${SHUNIT_TRUE} +} + +# Resume the normal execution of test cases +_shunit_endSkippingTest() { + __shunit_skip_test=${SHUNIT_FALSE} +} + # Force remaining assert and fail functions to be "skipped". # # This function forces the remaining assert and fail functions to be "skipped", @@ -1112,8 +1153,15 @@ _shunit_execSuite() { for _shunit_test_ in ${__shunit_suite}; do __shunit_testSuccess=${SHUNIT_TRUE} - # Disable skipping asserts. + # Disable skipping endSkippingAsserts + _shunit_endSkippingTest + + # Reset the counters + __shunit_assertsTotalInTest=0 + __shunit_assertsPassedInTest=0 + __shunit_assertsFailedInTest=0 + __shunit_assertsSkippedInTest=0 # Execute the per-test setUp() function. if ! setUp; then @@ -1134,7 +1182,9 @@ _shunit_execSuite() { fi # Update stats. - if ${__SHUNIT_BUILTIN} [ ${__shunit_testSuccess} -eq ${SHUNIT_TRUE} ]; then + if ${__SHUNIT_BUILTIN} [ ${__shunit_skip_test} -eq ${SHUNIT_TRUE} ]; then + __shunit_testsSkipped=`expr ${__shunit_testsSkipped} + 1` + elif ${__SHUNIT_BUILTIN} [ ${__shunit_testSuccess} -eq ${SHUNIT_TRUE} ]; then __shunit_testsPassed=`expr ${__shunit_testsPassed} + 1` else __shunit_testsFailed=`expr ${__shunit_testsFailed} + 1` @@ -1144,6 +1194,30 @@ _shunit_execSuite() { unset _shunit_test_ } +# Generates the skipped tests note +_shunit_generateSkippedTestsAndAssertsMessage() { + _shunit_skipped_msg_prefix_="$1" + _shunit_skipped_msg_suffix_="$2" + _shunit_skipped_msg_="" + if ${__SHUNIT_BUILTIN} [ "${__shunit_testsSkipped}" -gt 0 ]; then + _shunit_skipped_msg_="${_shunit_skipped_msg_prefix_}${__shunit_ansi_yellow}skipped tests=${__shunit_testsSkipped}${__shunit_ansi_none}" + fi + if ${__SHUNIT_BUILTIN} [ "${__shunit_assertsSkipped}" -gt 0 ]; then + if ${__SHUNIT_BUILTIN} [ "${_shunit_skipped_msg_}" != "" ]; then + _shunit_skipped_msg_="${_shunit_skipped_msg_}," + else + _shunit_skipped_msg_="${_shunit_skipped_msg_prefix_}" + fi + _shunit_skipped_msg_="${_shunit_skipped_msg_}${__shunit_ansi_yellow}skipped asserts=${__shunit_assertsSkipped}${__shunit_ansi_none}" + fi + printf "%s" "${_shunit_skipped_msg_}" + if ${__SHUNIT_BUILTIN} [ "${_shunit_skipped_msg_}" != "" ]; then + echo "${_shunit_skipped_msg_suffix_}" + fi + + unset _shunit_skipped_msg_prefix_ _shunit_skipped_msg_suffix_ _shunit_skipped_msg_ +} + # Generates the user friendly report with appropriate OK/FAILED message. # # Args: @@ -1170,25 +1244,24 @@ _shunit_generateReport() { ${__SHUNIT_CMD_ECHO_ESC} "${_shunit_msg_} tests." fi + _shunit_skipped_msg_="" if ${__SHUNIT_BUILTIN} [ ${_shunit_ok_} -eq ${SHUNIT_TRUE} ]; then _shunit_msg_="${__shunit_ansi_green}OK${__shunit_ansi_none}" - if ${__SHUNIT_BUILTIN} [ "${__shunit_assertsSkipped}" -gt 0 ]; then - _shunit_msg_="${_shunit_msg_} (${__shunit_ansi_yellow}skipped asserts=${__shunit_assertsSkipped}${__shunit_ansi_none})" - fi + _shunit_skipped_msg_=`_shunit_generateSkippedTestsAndAssertsMessage " (" ")"` + _shunit_msg_="${_shunit_msg_}${_shunit_skipped_msg_}" else _shunit_msg_="${__shunit_ansi_red}FAILED${__shunit_ansi_none}" - _shunit_msg_="${_shunit_msg_} (${__shunit_ansi_red}failures=${__shunit_assertsFailed}${__shunit_ansi_none}" - if ${__SHUNIT_BUILTIN} [ "${__shunit_assertsSkipped}" -gt 0 ]; then - _shunit_msg_="${_shunit_msg_},${__shunit_ansi_yellow}skipped asserts=${__shunit_assertsSkipped}${__shunit_ansi_none}" - fi - _shunit_msg_="${_shunit_msg_})" + _shunit_msg_="${_shunit_msg_} (${__shunit_ansi_red}failed tests=${__shunit_testsFailed}${__shunit_ansi_none}" + _shunit_msg_="${_shunit_msg_},${__shunit_ansi_red}failed asserts=${__shunit_assertsFailed}${__shunit_ansi_none}" + _shunit_skipped_msg_=`_shunit_generateSkippedTestsAndAssertsMessage "," ")"` + _shunit_msg_="${_shunit_msg_}${_shunit_skipped_msg_}" fi echo ${__SHUNIT_CMD_ECHO_ESC} "${_shunit_msg_}" __shunit_reportGenerated=${SHUNIT_TRUE} - unset _shunit_msg_ _shunit_ok_ + unset _shunit_msg_ _shunit_ok_ _shunit_skipped_msg_ } # Test for whether a function should be skipped. @@ -1198,6 +1271,9 @@ _shunit_generateReport() { # Returns: # boolean: whether the test should be skipped (TRUE/FALSE constant) _shunit_shouldSkip() { + if ${__SHUNIT_BUILTIN} test ${__shunit_skip_test} -eq ${SHUNIT_TRUE}; then + return ${SHUNIT_TRUE} + fi if ${__SHUNIT_BUILTIN} test ${__shunit_skip_asserts} -eq ${SHUNIT_FALSE}; then return ${SHUNIT_FALSE} fi @@ -1209,8 +1285,10 @@ _shunit_shouldSkip() { # Args: # None _shunit_assertPass() { - __shunit_assertsPassed=`expr ${__shunit_assertsPassed} + 1` - __shunit_assertsTotal=`expr ${__shunit_assertsTotal} + 1` + __shunit_assertsPassed=`expr "${__shunit_assertsPassed}" + 1` + __shunit_assertsPassedInTest=`expr "${__shunit_assertsPassedInTest}" + 1` + __shunit_assertsTotal=`expr "${__shunit_assertsTotal}" + 1` + __shunit_assertsTotalInTest=`expr "${__shunit_assertsTotalInTest}" + 1` } # Records a test failure. @@ -1232,7 +1310,9 @@ _shunit_assertFail() { # none _shunit_incFailedCount() { __shunit_assertsFailed=`expr "${__shunit_assertsFailed}" + 1` + __shunit_assertsFailedInTest=`expr "${__shunit_assertsFailedInTest}" + 1` __shunit_assertsTotal=`expr "${__shunit_assertsTotal}" + 1` + __shunit_assertsTotalInTest=`expr "${__shunit_assertsTotalInTest}" + 1` } # Records a skipped test. @@ -1241,20 +1321,30 @@ _shunit_incFailedCount() { # None _shunit_assertSkip() { __shunit_assertsSkipped=`expr "${__shunit_assertsSkipped}" + 1` + __shunit_assertsSkippedInTest=`expr "${__shunit_assertsSkippedInTest}" + 1` __shunit_assertsTotal=`expr "${__shunit_assertsTotal}" + 1` + __shunit_assertsTotalInTest=`expr "${__shunit_assertsTotalInTest}" + 1` } # Dump the current test metrics. # # Args: # none +_shunit_test_case_metrics() { + echo "< " \ +"total: ${__shunit_assertsTotalInTest} " \ +"passed: ${__shunit_assertsPassedInTest} " \ +"failed: ${__shunit_assertsFailedInTest} " \ +"skipped: ${__shunit_assertsSkippedInTest} " \ +" >" +} _shunit_metrics() { - echo "< \ -total: ${__shunit_assertsTotal} \ -passed: ${__shunit_assertsPassed} \ -failed: ${__shunit_assertsFailed} \ -skipped: ${__shunit_assertsSkipped} \ ->" + echo "< " \ +"total: ${__shunit_assertsTotal} " \ +"passed: ${__shunit_assertsPassed} " \ +"failed: ${__shunit_assertsFailed} " \ +"skipped: ${__shunit_assertsSkipped} " \ +" >" } # Prepare a script filename for sourcing. diff --git a/shunit2_general_test.sh b/shunit2_general_test.sh index 0415d56..c6d245a 100755 --- a/shunit2_general_test.sh +++ b/shunit2_general_test.sh @@ -50,7 +50,7 @@ testStartSkippingAssertsWithMessage() { #SHUNIT_COLOR='none' #. ${TH_SHUNIT} EOF - # Ignoring errors with `|| :` as we only care about `FAILED` in the output. + # Ignoring errors with `|| :` as we only care about `skipping asserts` in stderr. ( exec "${SHELL:-sh}" "${unittestF}" >"${stdoutF}" 2>"${stderrF}" ) || : if ! grep '\[skipping asserts\] SKIP-a-Dee-Doo-Dah' "${stderrF}" >/dev/null; then fail 'skipping message was not generated' @@ -68,7 +68,7 @@ testStartSkippingAssertsWithoutMessage() { #SHUNIT_COLOR='none' #. ${TH_SHUNIT} EOF - # Ignoring errors with `|| :` as we only care about `FAILED` in the output. + # Ignoring errors with `|| :` as we only care about `skipping asserts` in stderr. ( exec "${SHELL:-sh}" "${unittestF}" >"${stdoutF}" 2>"${stderrF}" ) || : if grep '\[skipping asserts\]' "${stderrF}" >/dev/null; then fail 'skipping message was unexpectedly generated' @@ -76,6 +76,74 @@ EOF return 0 } +testSkippingTests() { + unittestF="${SHUNIT_TMPDIR}/unittest" + sed 's/^#//' >"${unittestF}" <<\EOF +## Start skipping with a message. +#testSkipping() { +# skipTest "Example of test skipping" +# assertFalse "TRUE should be FALSE?" "${SHUNIT_TRUE}" +# assertTrue "FALSE should be TRUE?" "${SHUNIT_FALSE}" +#} +#SHUNIT_COLOR='none' +#. ${TH_SHUNIT} +EOF + # Ignoring errors with `|| :` as we only care about `skipping tests` in stderr + ( exec "${SHELL:-sh}" "${unittestF}" >"${stdoutF}" 2>"${stderrF}" ) || : + if ! grep '\[skipping test\]' "${stderrF}" >/dev/null; then + fail 'skipping test message was not generated' + fi + return 0 +} + +testSkippingTestsWithAssertSkippingEnabledBeforeSkipTest() { + unittestF="${SHUNIT_TMPDIR}/unittest" + sed 's/^#//' >"${unittestF}" <<\EOF +## Start skipping with a message. +#testSkipping() { +# startSkippingAsserts +# skipTest "Example of test skipping" +# assertFalse "TRUE should be FALSE?" "${SHUNIT_TRUE}" +# assertTrue "FALSE should be TRUE?" "${SHUNIT_FALSE}" +#} +#SHUNIT_COLOR='none' +#. ${TH_SHUNIT} +EOF + # Ignoring errors with `|| :` as we only care about `` in stderr. + ( exec "${SHELL:-sh}" "${unittestF}" >"${stdoutF}" 2>"${stderrF}" ) || : + if ! grep 'skipTest function MUST be called as the first thing in the test function!' "${stderrF}" >/dev/null; then + fail 'skipping test error message was not generated' + fi + if ! grep 'FAILED' "${stdoutF}" >/dev/null; then + fail 'test should have failed' + fi + return 0 +} + +testSkippingTestsWithAssertBeforeSkipTest() { + unittestF="${SHUNIT_TMPDIR}/unittest" + sed 's/^#//' >"${unittestF}" <<\EOF +## Start skipping with a message. +#testSkipping() { +# assertTrue "TRUE-TRUE" "${SHUNIT_TRUE}" +# skipTest "Example of test skipping" +# assertFalse "TRUE should be FALSE?" "${SHUNIT_TRUE}" +# assertTrue "FALSE should be TRUE?" "${SHUNIT_FALSE}" +#} +#SHUNIT_COLOR='none' +#. ${TH_SHUNIT} +EOF + # Ignoring errors with `|| :` as we only care about `` in stderr. + ( exec "${SHELL:-sh}" "${unittestF}" >"${stdoutF}" 2>"${stderrF}" ) || : + if ! grep 'skipTest function MUST be called as the first thing in the test function!' "${stderrF}" >/dev/null; then + fail 'skipping test error message was not generated' + fi + if ! grep 'FAILED' "${stdoutF}" >/dev/null; then + fail 'test should have failed' + fi + return 0 +} + setUp() { for f in "${stdoutF}" "${stderrF}"; do cp /dev/null "${f}"