Skip to content

Commit

Permalink
Merge pull request #92 from cmayo/main
Browse files Browse the repository at this point in the history
add setUp, setUpBeforeScript, tearDown and tearDownAfterScript functions execution
  • Loading branch information
khru authored Sep 15, 2023
2 parents fbcf09b + 32ee185 commit 8c7e0bd
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 8 additions & 0 deletions src/helpers.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
20 changes: 20 additions & 0 deletions src/runner.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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++))
Expand All @@ -39,6 +41,22 @@ function runTest() {
fi
}

function runSetUp() {
executeFunctionIfExists 'setUp'
}

function runSetUpBeforeScript() {
executeFunctionIfExists 'setUpBeforeScript'
}

function runTearDown() {
executeFunctionIfExists 'tearDown'
}

function runTearDownAfterScript() {
executeFunctionIfExists 'tearDownAfterScript'
}

###############
#### MAIN #####
###############
Expand Down Expand Up @@ -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
}

Expand Down
16 changes: 16 additions & 0 deletions tests/unit/helpers_test.sh
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
#!/bin/bash

function dummyFunction() {
echo "dummyFunction executed"
}

function test_normalizeTestFunctionName_empty() {
assertEquals "" "$(normalizeTestFunctionName)"
}
Expand Down Expand Up @@ -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")"
}
28 changes: 28 additions & 0 deletions tests/unit/setup_teardown_test.sh
Original file line number Diff line number Diff line change
@@ -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"
}

0 comments on commit 8c7e0bd

Please sign in to comment.