From f1d02509c4757c577b6efd1faf6e1e516184b5a6 Mon Sep 17 00:00:00 2001 From: Carlos Mayo Date: Fri, 15 Sep 2023 15:41:30 +0200 Subject: [PATCH 1/2] add setUp, setUpBeforeScript, tearDown and tearDownAfterScript functions execution --- CHANGELOG.md | 1 + src/helpers.sh | 8 ++++++++ src/runner.sh | 20 ++++++++++++++++++++ tests/unit/helpers_test.sh | 16 ++++++++++++++++ tests/unit/setup_teardown_test.sh | 28 ++++++++++++++++++++++++++++ 5 files changed, 73 insertions(+) create mode 100644 tests/unit/setup_teardown_test.sh diff --git a/CHANGELOG.md b/CHANGELOG.md index 30cd0af5..8edb3160 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -15,6 +15,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..80711fa8 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' + + assertEquals "" "$(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" +} + From 32ee185de99561736ed1007d679dd61b4a43ad1f Mon Sep 17 00:00:00 2001 From: Carlos Mayo Date: Fri, 15 Sep 2023 16:16:00 +0200 Subject: [PATCH 2/2] Use assertEmpty executionFunctionIfExists test --- tests/unit/helpers_test.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/unit/helpers_test.sh b/tests/unit/helpers_test.sh index 80711fa8..85575046 100644 --- a/tests/unit/helpers_test.sh +++ b/tests/unit/helpers_test.sh @@ -53,5 +53,5 @@ function test_dummyFunction_is_executed_with_execute_function_if_exists() { function test_no_function_is_executed_with_execute_function_if_exists() { local function_name='notExistingFunction' - assertEquals "" "$(executeFunctionIfExists "$function_name")" + assertEmpty "$(executeFunctionIfExists "$function_name")" }