Skip to content

Commit

Permalink
Put setup and teardown of django env into a plugin to allow coverage …
Browse files Browse the repository at this point in the history
…tracking

Refs #2
  • Loading branch information
rozza authored and Jeff Balogh committed Jul 14, 2010
1 parent bda1f78 commit 29f3092
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 7 deletions.
25 changes: 25 additions & 0 deletions django_nose/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,28 @@ class ResultPlugin(object):

def finalize(self, result):
self.result = result


class DjangoSetUpPlugin(object):
"""
Configures Django to setup and tear down the environment.
This allows coverage to report on all code imported and used during the
initialisation of the test runner.
"""

name = "django setup"
enabled = True

def __init__(self, runner):
super(DjangoSetUpPlugin, self).__init__()
self.runner = runner

def begin(self):
"""Setup the environment"""
self.runner.setup_test_environment()
self.old_names = self.runner.setup_databases()

def finalize(self, result):
"""Destroy the environment"""
self.runner.teardown_databases(self.old_names)
self.runner.teardown_test_environment()
11 changes: 4 additions & 7 deletions django_nose/runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

import nose.core

from django_nose.plugin import ResultPlugin
from django_nose.plugin import DjangoSetUpPlugin, ResultPlugin

try:
any
Expand All @@ -35,9 +35,11 @@ def any(iterable):
class NoseTestSuiteRunner(DjangoTestSuiteRunner):

def run_suite(self, nose_argv):
django_setup_plugin = DjangoSetUpPlugin(self)

result_plugin = ResultPlugin()
nose.core.TestProgram(argv=nose_argv, exit=False,
addplugins=[result_plugin])
addplugins=[django_setup_plugin, result_plugin])
return result_plugin.result

def run_tests(self, test_labels, extra_tests=None):
Expand All @@ -56,9 +58,6 @@ def run_tests(self, test_labels, extra_tests=None):
Returns the number of tests that failed.
"""
self.setup_test_environment()
old_names = self.setup_databases()

nose_argv = ['nosetests', '--verbosity', str(self.verbosity)] + list(test_labels)
if hasattr(settings, 'NOSE_ARGS'):
nose_argv.extend(settings.NOSE_ARGS)
Expand All @@ -77,8 +76,6 @@ def run_tests(self, test_labels, extra_tests=None):
print ' '.join(nose_argv)

result = self.run_suite(nose_argv)
self.teardown_databases(old_names)
self.teardown_test_environment()
# suite_result expects the suite as the first argument. Fake it.
return self.suite_result({}, result)

Expand Down

0 comments on commit 29f3092

Please sign in to comment.