Skip to content

Commit

Permalink
Models defined anywhere that's run at test-loading time now get their…
Browse files Browse the repository at this point in the history
… DB tables properly created. Fixes #15.
  • Loading branch information
Erik Rose committed May 17, 2012
1 parent 267b87e commit 178a8a5
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 6 deletions.
18 changes: 16 additions & 2 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@ Features
be in ``INSTALLED_APPS``
* Running the tests in one or more specific modules (or apps, or classes, or
running a specific test)
* Obviating the need to import all your tests into ``tests/__init__.py``
* Obviating the need to import all your tests into ``tests/__init__.py``.
This not only saves busy-work but also eliminates the possibility of
accidentally shadowing test classes.
* Taking advantage of all the useful `nose plugins`_
* Fixture bundling, an optional feature which speeds up your fixture-based
tests by a factor of 4
Expand Down Expand Up @@ -88,7 +90,7 @@ by eliminating redundant setup of Django test fixtures. To use it...
cause fixtures to load once per class rather than once per test.
2. Activate fixture bundling by passing the ``--with-fixture-bundling`` option
to ``./manage.py test``. This loads each unique set of fixtures only once,
even across class boundaries.
even across class, module, and app boundaries.

How Fixture Bundling Works
~~~~~~~~~~~~~~~~~~~~~~~~~~
Expand Down Expand Up @@ -179,6 +181,16 @@ ultimately acts more like a TestCase than a TransactionTestCase.
.. _can leave the DB in an unclean state: https://docs.djangoproject.com/en/dev/topics/testing/?from=olddocs#django.test.TransactionTestCase


Test-Only Models
----------------

If you have a model that is used only by tests (for example, to test an
abstract model base class), you can put it in any file that's imported in the
course of loading tests. For example, if the tests that need it are in
``test_models.py``, you can put the model in there, too. django-nose will make
sure its DB table gets created.


Using With South
----------------

Expand Down Expand Up @@ -260,6 +272,8 @@ Recent Version History
these tests run last. Now django-nose does too. This means one fewer source
of failures on existing projects. (Erik Rose)
* Add support for hygienic TransactionTestCases.
* Support models that are used for tests only. Just put them in any file
imported in the course of loading tests.
* Made the fixture bundler more conservative, fixing some conceivable
situations in which fixtures would not appear as intended if a
TransactionTestCase found its way into the middle of a bundle. (Erik Rose)
Expand Down
19 changes: 16 additions & 3 deletions django_nose/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ class ResultPlugin(AlwaysOnPlugin):
``result`` after running the tests to get the TestResult object.
"""
name = "result"
name = 'result'

def finalize(self, result):
self.result = result
Expand All @@ -50,14 +50,27 @@ class DjangoSetUpPlugin(AlwaysOnPlugin):
initialization of the test runner.
"""
name = "django setup"
name = 'django setup'

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

def begin(self):
def prepareTest(self, test):
"""Create the Django DB and model tables, and do other setup.
This isn't done in begin() because that's too early--the DB has to be
set up *after* the tests are imported so the model registry contains
models defined in tests.py modules. Models are registered at
declaration time by their metaclass.
prepareTestRunner() might also have been a sane choice, except that, if
some plugin returns something from it, none of the other ones get
called. I'd rather not dink with scores if I don't have to.
"""
# What is this stdout switcheroo for?
sys_stdout = sys.stdout
sys.stdout = self.sys_stdout

Expand Down
2 changes: 1 addition & 1 deletion django_nose/runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@ def _skip_create_test_db(self, verbosity=1, autoclobber=False):

def _reusing_db():
"""Return whether the ``REUSE_DB`` flag was passed"""
return (os.getenv('REUSE_DB', 'false').lower() in ('true', '1', ''))
return os.getenv('REUSE_DB', 'false').lower() in ('true', '1', '')


def _can_support_reuse_db(connection):
Expand Down

0 comments on commit 178a8a5

Please sign in to comment.