Skip to content
Merged
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
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")"
Copy link
Member

@khru khru Sep 15, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This function does not exist assertEmpty may be you mean assertNotEquals

Copy link
Collaborator Author

@cmayo cmayo Sep 15, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, you are right. I'm trying to check why I didn't get any error with this non-existent method

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, thanks to you, We've just added the assert #93 (it needs a refactor, but the first step is there)

}
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"
}