From b43aadad8bce87a2b387283510cfd94901c30e5d Mon Sep 17 00:00:00 2001 From: Tim Abbott Date: Sat, 23 Jan 2016 14:16:14 -0800 Subject: [PATCH] test-backend: Rewrite in python to support computing test coverage. The code for doing test coverage is just a lot cleaner this way over adding it to the shell script version. Based on the basic test runner code here: https://docs.djangoproject.com/en/1.9/topics/testing/advanced/ --- requirements.txt | 1 + tools/test-backend | 61 +++++++++++++++++++++++++++++++++++----------- 2 files changed, 48 insertions(+), 14 deletions(-) diff --git a/requirements.txt b/requirements.txt index b775e408d6..16537b0912 100644 --- a/requirements.txt +++ b/requirements.txt @@ -12,6 +12,7 @@ boto==2.38.0 certifi==2015.4.28 cffi==1.1.2 chardet==2.3.0 +coverage==4.0.3 cryptography==0.9.1 defusedxml==0.4.1 diff-match-patch==20121119 diff --git a/tools/test-backend b/tools/test-backend index 2533781a8a..03b78c4a0f 100755 --- a/tools/test-backend +++ b/tools/test-backend @@ -1,17 +1,50 @@ -#!/usr/bin/env bash -set -e +#!/usr/bin/env python2.7 -target='zerver' +import optparse +import os +import sys -case "$1" in - zerver*) - target="$1" - shift - ;; -esac +import django +from django.conf import settings +from django.test.utils import get_runner -cd "$(dirname "$0")"/.. -./tools/generate-fixtures -# "-u" uses unbuffered IO, which is important when wrapping it in subprocess -export PYTHONUNBUFFERED="y" -/usr/bin/env python2.7 ./manage.py test "$target" --settings=zproject.test_settings "$@" +if __name__ == "__main__": + sys.path.insert(0, os.path.join(os.path.dirname(__file__), '..')) + os.environ['DJANGO_SETTINGS_MODULE'] = 'zproject.test_settings' + # "-u" uses unbuffered IO, which is important when wrapping it in subprocess + os.environ['PYTHONUNBUFFERED'] = 'y' + django.setup() + + parser = optparse.OptionParser() + parser.add_option('--coverage', dest='coverage', + action="store_true", + default=False, help='Compute test coverage.') + + (options, args) = parser.parse_args() + if len(args) == 0: + suites = ["zerver"] + else: + suites = args + + if options.coverage: + import coverage + cov = coverage.Coverage() + cov.start() + + TestRunner = get_runner(settings) + test_runner = TestRunner() + failures = test_runner.run_tests(suites) + + if options.coverage: + cov.stop() + cov.save() + print("Printing coverage data") + cov.report(show_missing=False) + cov.html_report() + print("HTML report saved to htmlcov/") + + if failures: + print('FAILED!') + else: + print('DONE!') + sys.exit(bool(failures))