Created
September 1, 2017 00:52
-
-
Save tadeoos/16346c5bdcbeb2531083614184cc4ff1 to your computer and use it in GitHub Desktop.
Snippet for creating custom manage.py test command to wrap normal tests with coverage
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import subprocess | |
from django.core.management import BaseCommand | |
class Command(BaseCommand): | |
help = "Run tests with coverage. See options for more actions." | |
def add_arguments(self, parser): | |
# Positional arguments | |
# parser.add_argument('poll_id', nargs='+', type=int) | |
# Named (optional) arguments | |
parser.add_argument( | |
'--full', | |
action='store_true', | |
dest='full', | |
default=False, | |
help='Generate html report and bagde for readme.', | |
) | |
parser.add_argument( | |
'--html', | |
action='store_true', | |
dest='html', | |
default=False, | |
help='Generate html report.', | |
) | |
parser.add_argument( | |
'--badge', | |
action='store_true', | |
dest='badge', | |
default=False, | |
help='Generate bagde for readme.', | |
) | |
# A command must define handle() | |
def handle(self, *args, **options): | |
if options['verbosity'] > 1: | |
self.stdout.write("--- Running tests...\n\n") | |
verbos = "-v{}".format(options['verbosity']) | |
subprocess.call(["coverage", "run", "./manage.py", "test", verbos]) | |
if options['full']: | |
options['html'] = True | |
options['badge'] = True | |
if options['html']: | |
if options['verbosity'] > 1: | |
self.stdout.write("\n--- Building HTML report...\n") | |
subprocess.call(["coverage", "html"]) | |
if options['verbosity'] > 1: | |
self.stdout.write("... done") | |
if options['badge']: | |
if options['verbosity'] > 1: | |
self.stdout.write("\n--- Generating coverage badge...\n") | |
try: | |
subprocess.call(["rm", "coverage.svg"]) | |
except: | |
pass | |
subprocess.call(["coverage-badge", "-o", "coverage.svg"]) | |
if options['verbosity'] > 0: | |
self.stdout.write("--- Site tests completed ---\n") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment