From 48d6beaeef30d22e9366fdf7fa5d3311da66bf89 Mon Sep 17 00:00:00 2001 From: Scott Date: Mon, 24 Jan 2022 13:47:27 -0800 Subject: [PATCH] Adds optional code coverage reporting to run_tests.sh. --- .gitignore | 1 + requirements.txt | 1 + tests/acl_test.py | 3 +-- tests/dict_utils_test.py | 1 + tests/run_tests.sh | 31 +++++++++++++++++++++++++++---- 5 files changed, 31 insertions(+), 6 deletions(-) diff --git a/.gitignore b/.gitignore index 221b64b..649e578 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ +.coverage scott_secrets.py __pycache__/* */__pycache__/* diff --git a/requirements.txt b/requirements.txt index df7bb5f..9962ad5 100644 --- a/requirements.txt +++ b/requirements.txt @@ -2,6 +2,7 @@ aiohttp antlr4-python3-runtime bitstring cloudpickle +coverage holidays nltk numpy diff --git a/tests/acl_test.py b/tests/acl_test.py index 4c1cf21..1e9dcbf 100755 --- a/tests/acl_test.py +++ b/tests/acl_test.py @@ -4,7 +4,7 @@ import re import unittest import acl -import bootstrap +import unittest_utils # Needed for --unittests_ignore_perf flag class TestSimpleACL(unittest.TestCase): @@ -89,5 +89,4 @@ class TestSimpleACL(unittest.TestCase): if __name__ == '__main__': - unittest.main = bootstrap.initialize(unittest.main) unittest.main() diff --git a/tests/dict_utils_test.py b/tests/dict_utils_test.py index 1bdbb9b..9fb8157 100755 --- a/tests/dict_utils_test.py +++ b/tests/dict_utils_test.py @@ -3,6 +3,7 @@ import unittest import dict_utils as du +import unittest_utils # Needed for --unittests_ignore_perf flag class TestDictUtils(unittest.TestCase): diff --git a/tests/run_tests.sh b/tests/run_tests.sh index 5711b9a..db9f8cb 100755 --- a/tests/run_tests.sh +++ b/tests/run_tests.sh @@ -38,7 +38,7 @@ make_header() { } function usage() { - echo "Usage: $0 [-a]|[-i][-u][-d]" + echo "Usage: $0 [-a]|[-i][-u][-d] [--coverage]" echo echo "Runs tests under ${ROOT}. Options control which test types:" echo @@ -67,6 +67,9 @@ while [[ $# -gt 0 ]]; do -i|--integration) INTEGRATION=1 ;; + --coverage) + COVERAGE=1 + ;; *) # unknown option echo "Argument $key was not recognized." echo @@ -82,6 +85,9 @@ if [ $(expr ${DOCTEST} + ${UNITTEST} + ${INTEGRATION}) -eq 0 ]; then exit 2 fi +if [ ${COVERAGE} -eq 1 ]; then + coverage erase +fi FAILED_TESTS="" if [ ${DOCTEST} -eq 1 ]; then @@ -90,7 +96,11 @@ if [ ${DOCTEST} -eq 1 ]; then BASE=$(basename ${doctest}) BASE="${BASE} (doctest)" make_header "${BASE}" "${CYAN}" - OUT=$( python3 ${doctest} 2>&1 ) + if [ ${COVERAGE} -eq 1 ]; then + OUT=$( coverage run --source ${HOME}/lib --append ${doctest} 2>&1 ) + else + OUT=$( python3 ${doctest} 2>&1 ) + fi FAILED=$( echo "${OUT}" | grep '\*\*\*Test Failed\*\*\*' | wc -l ) if [ $FAILED == 0 ]; then echo "OK" @@ -108,7 +118,11 @@ if [ ${UNITTEST} -eq 1 ]; then BASE=$(basename ${test}) BASE="${BASE} (unittest)" make_header "${BASE}" "${GREEN}" - ${test} + if [ ${COVERAGE} -eq 1 ]; then + coverage run --source ${HOME}/lib --append ${test} --unittests_ignore_perf + else + ${test} + fi if [ $? -ne 0 ]; then FAILURES=$((FAILURES+1)) FAILED_TESTS="${FAILED_TESTS},${BASE} (python3 ${test})" @@ -121,7 +135,11 @@ if [ ${INTEGRATION} -eq 1 ]; then BASE=$(basename ${test}) BASE="${BASE} (integration test)" make_header "${BASE}" "${ORANGE}" - ${test} + if [ ${COVERAGE} -eq 1 ]; then + coverage run --source ${HOME}/lib --append ${test} + else + ${test} + fi if [ $? -ne 0 ]; then FAILURES=$((FAILURES+1)) FAILED_TESTS="${FAILED_TESTS},${BASE} (python3 ${test})" @@ -129,6 +147,11 @@ if [ ${INTEGRATION} -eq 1 ]; then done fi +if [ ${COVERAGE} -eq 1 ]; then + make_header "Code Coverage Report" "${GREEN}" + coverage report --omit=config-3.8.py --sort=-cover +fi + if [ ${FAILURES} -ne 0 ]; then FAILED_TESTS=$(echo ${FAILED_TESTS} | sed 's/^,/__/g') FAILED_TESTS=$(echo ${FAILED_TESTS} | sed 's/,/\n__/g') -- 2.46.0