From 39668b768ad0b78a4f2a4d26314ac4fb9418f3b6 Mon Sep 17 00:00:00 2001 From: Omer Katz Date: Sat, 26 Jan 2013 18:10:39 +0300 Subject: [PATCH] Added an option to run tests without code coverage. --- .gitignore | 1 + README.rst | 9 +++++++++ discoverage/management/__init__.py | 0 discoverage/management/commands/__init__.py | 0 .../management/commands/erasecoveragedata.py | 8 ++++++++ discoverage/management/commands/test.py | 17 +++++++++++++++++ discoverage/runner.py | 8 ++++++++ 7 files changed, 43 insertions(+) create mode 100644 discoverage/management/__init__.py create mode 100644 discoverage/management/commands/__init__.py create mode 100644 discoverage/management/commands/erasecoveragedata.py create mode 100644 discoverage/management/commands/test.py diff --git a/.gitignore b/.gitignore index ce8563b..d863b10 100644 --- a/.gitignore +++ b/.gitignore @@ -6,3 +6,4 @@ build dist *.egg-info +.idea \ No newline at end of file diff --git a/README.rst b/README.rst index 62ee8dc..af112d1 100644 --- a/README.rst +++ b/README.rst @@ -10,6 +10,15 @@ Inspired by `django-coverage `_. Usage ----- +To run the tests type: + + ./manage.py test [options] [appname ...] + +To run the tests without code coverage (i.e. run django-discover-runner instead) type: + + ./manage.py test --no-coverage [options] [appname ...] + + One of the objectives of ``django-discover-runner`` is to allow the separation of a Django app's tests from the code it's testing. Since tests no longer reside in an app, ``django-discoverage`` needs a different way to know which apps to diff --git a/discoverage/management/__init__.py b/discoverage/management/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/discoverage/management/commands/__init__.py b/discoverage/management/commands/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/discoverage/management/commands/erasecoveragedata.py b/discoverage/management/commands/erasecoveragedata.py new file mode 100644 index 0000000..2ed28f9 --- /dev/null +++ b/discoverage/management/commands/erasecoveragedata.py @@ -0,0 +1,8 @@ +from coverage import coverage +from django.core.management import BaseCommand + +class Command(BaseCommand): + def handle(self, filename, **options): + cov = coverage(data_file=filename) + + cov.erase() diff --git a/discoverage/management/commands/test.py b/discoverage/management/commands/test.py new file mode 100644 index 0000000..5639648 --- /dev/null +++ b/discoverage/management/commands/test.py @@ -0,0 +1,17 @@ +from optparse import make_option +from django.core.management import CommandError + +try: + # Depends on a change that will be made on discover runner. + # It can work without it at the time being and therefor the try...except block exists. + # Also, even if there was no expected change, it is useful as a foresight. + from discover_runner.management.commands.test import Command as TestCommand +except ImportError: + from django.core.management.commands.test import Command as TestCommand + +class Command(TestCommand): + option_list = TestCommand.option_list + ( + make_option('--no-coverage', + action='store_false', dest='perform_coverage', default=True, + help='Specifies that no code coverage will be performed.'), + ) \ No newline at end of file diff --git a/discoverage/runner.py b/discoverage/runner.py index 0aa84fb..d9c0297 100644 --- a/discoverage/runner.py +++ b/discoverage/runner.py @@ -6,6 +6,11 @@ class DiscoverageRunner(DiscoverRunner): + def __init__(self, perform_coverage=True, **kwargs): + self.perform_coverage = perform_coverage + + super(DiscoverageRunner, self).__init__(**kwargs) + def build_suite(self, *args, **kwargs): if not hasattr(self, '_suite'): self._suite = super(DiscoverageRunner, self).build_suite( @@ -13,6 +18,9 @@ def build_suite(self, *args, **kwargs): return self._suite def run_tests(self, test_labels, extra_tests=None, **kwargs): + if not self.perform_coverage: + return super(DiscoverageRunner, self).run_tests(test_labels, extra_tests=extra_tests, **kwargs) + cov = coverage.coverage(omit=COVERAGE_OMIT_MODULES) for pattern in COVERAGE_EXCLUDE_PATTERNS: