diff --git a/CHANGELOG.md b/CHANGELOG.md index 6e21fab1..df777f21 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -17,6 +17,7 @@ - Fix error on count assertions - Added pipeline to add contributors to the readme - Added documentation with vitepress +- Added `setUp`, `setUpBeforeScript`, `tearDown` and `tearDownAfterScript` function execution before and/or after test and/or script execution ### 0.5.0 ### 2023-09-10 diff --git a/src/helpers.sh b/src/helpers.sh index b447d956..a90f2676 100755 --- a/src/helpers.sh +++ b/src/helpers.sh @@ -51,3 +51,11 @@ function getFunctionsToRun() { echo "${functions_to_run[@]}" } + +function executeFunctionIfExists() { + local function_name=$1 + + if declare -F | awk '{print $3}' | grep -Eq "^${function_name}$"; then + "$function_name" + fi +} diff --git a/src/runner.sh b/src/runner.sh index 7cc3dd5c..ca4e86dc 100755 --- a/src/runner.sh +++ b/src/runner.sh @@ -28,7 +28,9 @@ function runTest() { local function_name="$1" local current_assertions_failed="$_ASSERTIONS_FAILED" + runSetUp "$function_name" + runTearDown if [ "$current_assertions_failed" == "$_ASSERTIONS_FAILED" ]; then ((_TESTS_PASSED++)) @@ -39,6 +41,22 @@ function runTest() { fi } +function runSetUp() { + executeFunctionIfExists 'setUp' +} + +function runSetUpBeforeScript() { + executeFunctionIfExists 'setUpBeforeScript' +} + +function runTearDown() { + executeFunctionIfExists 'tearDown' +} + +function runTearDownAfterScript() { + executeFunctionIfExists 'tearDownAfterScript' +} + ############### #### MAIN ##### ############### @@ -77,10 +95,12 @@ function loadTestFiles() { fi # shellcheck disable=SC1090 source "$test_file" + runSetUpBeforeScript callTestFunctions "$test_file" "$_FILTER" if [ "$PARALLEL_RUN" = true ] ; then wait fi + runTearDownAfterScript done } diff --git a/tests/unit/helpers_test.sh b/tests/unit/helpers_test.sh index 2a64a8a4..85575046 100644 --- a/tests/unit/helpers_test.sh +++ b/tests/unit/helpers_test.sh @@ -1,5 +1,9 @@ #!/bin/bash +function dummyFunction() { + echo "dummyFunction executed" +} + function test_normalizeTestFunctionName_empty() { assertEquals "" "$(normalizeTestFunctionName)" } @@ -39,3 +43,15 @@ function test_getFunctionsToRun_fail_when_duplicates() { assertGeneralError "$(getFunctionsToRun "prefix" "" "${functions[*]}")" } + +function test_dummyFunction_is_executed_with_execute_function_if_exists() { + local function_name='dummyFunction' + + assertEquals "dummyFunction executed" "$(executeFunctionIfExists "$function_name")" +} + +function test_no_function_is_executed_with_execute_function_if_exists() { + local function_name='notExistingFunction' + + assertEmpty "$(executeFunctionIfExists "$function_name")" +} diff --git a/tests/unit/setup_teardown_test.sh b/tests/unit/setup_teardown_test.sh new file mode 100644 index 00000000..77cbfd9d --- /dev/null +++ b/tests/unit/setup_teardown_test.sh @@ -0,0 +1,28 @@ +#!/bin/bash + +TEST_COUNTER=1 + +function setUpBeforeScript() { + TEST_COUNTER=$(( TEST_COUNTER + 1 )) +} + +function setUp() { + TEST_COUNTER=$(( TEST_COUNTER + 1 )) +} + +function tearDown() { + TEST_COUNTER=$(( TEST_COUNTER - 1 )) +} + +function tearDownAfterScript() { + TEST_COUNTER=$(( TEST_COUNTER - 1 )) +} + +function test_counter_is_incremented_after_setup_before_script_and_setup() { + assertEquals "3" "$TEST_COUNTER" +} + +function test_counter_is_decremented_and_incremented_after_teardown_and_setup() { + assertEquals "3" "$TEST_COUNTER" +} +