From 20a4310d4ff77bb03dabd1f61b99e953e0dd12b1 Mon Sep 17 00:00:00 2001 From: Emmanuel Valverde Ramos Date: Fri, 8 Sep 2023 17:23:02 +0200 Subject: [PATCH] chore: add execution time to the render results of the test --- src/console_results.sh | 6 ++++++ src/test_runner.sh | 1 + tests/unit/render_test.sh | 20 ++++++++++++++++++++ 3 files changed, 27 insertions(+) diff --git a/src/console_results.sh b/src/console_results.sh index 41bf31c3..c9c1ab5c 100644 --- a/src/console_results.sh +++ b/src/console_results.sh @@ -13,10 +13,16 @@ ${COLOR_FAINT}Total assertions:${COLOR_DEFAULT} ${COLOR_BOLD}${totalAssertions}$ if [ "$totalFailed" -gt 0 ]; then printf "${COLOR_FAINT}Total assertions failed:${COLOR_DEFAULT} ${COLOR_BOLD}${COLOR_FAILED}${totalFailed}${COLOR_DEFAULT}\n" + _TIME_TERMINATION=$((($(date +%s%N) - $_TIME_START)/1000000)) + printf "${COLOR_BOLD}%s${COLOR_DEFAULT}\n" "Time taken: ${_TIME_TERMINATION} ms" exit 1 else printf "${COLOR_ALL_PASSED}All assertions passed.${COLOR_DEFAULT}\n" fi + + _TIME_TERMINATION=$((($(date +%s%N) - $_TIME_START)/1000000)) + printf "${COLOR_BOLD}%s${COLOR_DEFAULT}\n" "Time taken: ${_TIME_TERMINATION} ms" + exit 0 } # Set a trap to call renderResult when the script exits diff --git a/src/test_runner.sh b/src/test_runner.sh index 9048ce17..7dc85437 100755 --- a/src/test_runner.sh +++ b/src/test_runner.sh @@ -7,6 +7,7 @@ _TOTAL_TESTS=0 # shellcheck disable=SC2155 # shellcheck disable=SC2034 callTestFunctions() { + _TIME_START=$(date +%s%N); local script="$1" local filter="$2" local prefix="test" diff --git a/tests/unit/render_test.sh b/tests/unit/render_test.sh index 1ad36a98..e818e1e7 100644 --- a/tests/unit/render_test.sh +++ b/tests/unit/render_test.sh @@ -60,3 +60,23 @@ function test_render_result_not_all_assertions_passed() { "All assertions passed"\ "$(renderResult $total_tests $assertions_passed $assertions_failed)" } + +function test_render_time_of_execution_when_all_assertions_passed() { + local total_tests=2 + local assertions_passed=5 + local assertions_failed=1 + + assertMatches\ + ".*Time taken: [[:digit:]]+ ms"\ + "$(renderResult $total_tests $assertions_passed $assertions_failed)" +} + +function test_render_time_of_execution_when_not_all_assertions_passed() { + local total_tests=2 + local assertions_passed=5 + local assertions_failed=1 + + assertMatches\ + ".*Time taken: [[:digit:]]+ ms"\ + "$(renderResult $total_tests $assertions_passed $assertions_failed)" +}