Skip to content

Instantly share code, notes, and snippets.

@AdrienLemaire
Created October 19, 2011 06:04
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save AdrienLemaire/1297575 to your computer and use it in GitHub Desktop.
Save AdrienLemaire/1297575 to your computer and use it in GitHub Desktop.
fab coverage
""""
Required: fabric, django, coverage, django-coverage, termcolor
""""
from coverage.misc import CoverageException
from termcolor import colored
def coverage(html=1):
"""Run coverage tests with html output, or
or return only the coverage percentage::
$ fab coverage
$ fab coverage:0 # only return percentage of code
This script insures that 80% of each file is covered, instead
of 80% for the whole project, that might pass with some files
having no tests at all.
"""
not_html = not int(html)
settings_file = (not_html and "coverage_nohtml_settings"
or "coverage_settings")
try:
results = local("./manage.py test_coverage --settings=%s %s" %
(settings_file, " ".join(settings.TEST_APPS)), capture=not_html)
except:
raise CoverageException(colored("You have failing tests, "
"run 'fab test' for more details", "red", attrs=["bold"]))
if not_html:
# Todo regex to get the coverage percentages, coz this code is awful
global_percentage = int(results.split("\n")[-3].split()[-1][:-1])
files_percents = dict((
line.split()[0], # filename
int(line.split()[3][:-1]), # file percentage
) for line in results.split("-" * 64)[1].split("\n")[1:-1])
errors = []
for name, percentage in files_percents.iteritems():
if percentage < 80:
errors.append(colored("The file %s is only covered to %d%%" % (
name, percentage), "red", attrs=["bold"]))
if errors:
raise CoverageException("\nThere isn't enought coverage:\n%s" %
"\n\t".join(errors))
else:
print(colored("Yeah, %d%% of the code is covered :)" %
global_percentage, "green", attrs=["bold"]))
else:
local("%s coverage_html/index.html" % settings.WWW_OPEN)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment