Skip to content
Snippets Groups Projects
Code owners
Assign users and groups as approvers for specific file changes. Learn more.
python-coverage.sh 1.29 KiB
#!/bin/bash

# Default lines to exclude in python-coverage
COVERAGE_EXCLUDE_LINES="[report]\nexclude_lines = \n  if __name__ == .__main__.\n  def main\n"

# Determine python-coverage executable
if type "coverage3" >& /dev/null; then
  COVERAGE=coverage3
elif type "python3-coverage" >& /dev/null; then
  COVERAGE=python3-coverage
else
  COVERAGE=""
fi

#
# Run a python test under python-coverage (if available).
#
# Usage:
#
#   python_coverage_test module mytest.py [testarg1 testarg2 ...]
#
function python_coverage_test {
  PYTHON_MODULE=$1
  shift

  if [ -n "$COVERAGE" ]; then
      #run test using python python-coverage tool

      #erase previous results
      $COVERAGE erase

      #setup python-coverage config file
      RCFILE=`basename $0`.python-coveragerc
      printf "$COVERAGE_EXCLUDE_LINES" > $RCFILE

      $COVERAGE run --rcfile $RCFILE --branch --include="*${PYTHON_MODULE}*" "$@"
      RESULT=$?
      if [ $RESULT -eq 0 ]; then
          echo " *** Code python-coverage results *** "
          $COVERAGE report -m
          echo " *** End python-coverage results *** "
      fi
      exit $RESULT
  else
      #python-coverage not available
      echo "Please run: 'pip3 install coverage' to enable code coverage reporting of the unit tests"
      #run plain test script
      python3 "$@"
  fi
}