#7835: Provide the ability for model definitions that are only availably during
testing
-------------------------------------+------------------------------------
     Reporter:  Russell Keith-Magee  |                    Owner:  nobody
         Type:  New feature          |                   Status:  new
    Component:  Testing framework    |                  Version:  master
     Severity:  Normal               |               Resolution:
     Keywords:  feature test models  |             Triage Stage:  Accepted
    Has patch:  0                    |      Needs documentation:  0
  Needs tests:  0                    |  Patch needs improvement:  0
Easy pickings:  0                    |                    UI/UX:  0
-------------------------------------+------------------------------------

Comment (by Ashley Waite):

 This was a particularly annoying issue to me, which I resolved by creating
 a new TestRunner that did the work for me.

 I think including something like this for easy use would effectively
 resolve this.

 {{{
 from importlib.util import find_spec
 import unittest

 from django.apps import apps
 from django.conf import settings
 from django.test.runner import DiscoverRunner


 class TestLoader(unittest.TestLoader):
     """ Loader that reports all successful loads to a runner """
     def __init__(self, *args, runner, **kwargs):
         self.runner = runner
         super().__init__(*args, **kwargs)

     def loadTestsFromModule(self, module, pattern=None):
         suite = super().loadTestsFromModule(module, pattern)
         if suite.countTestCases():
             self.runner.register_test_module(module)
         return suite


 class RunnerWithTestModels(DiscoverRunner):
     """ Test Runner that will add any test packages with a 'models' module
 to INSTALLED_APPS.
         Allows test only models to be defined within any package that
 contains tests.
         All test models should be set with app_label = 'tests'
     """
     def __init__(self, *args, **kwargs):
         self.test_packages = set()
         self.test_loader = TestLoader(runner=self)
         super().__init__(*args, **kwargs)

     def register_test_module(self, module):
         self.test_packages.add(module.__package__)

     def setup_databases(self, **kwargs):
         # Look for test models
         test_apps = set()
         for package in self.test_packages:
             if find_spec('.models', package):
                 test_apps.add(package)
         # Add test apps with models to INSTALLED_APPS that aren't already
 there
         new_installed = settings.INSTALLED_APPS + tuple(ta for ta in
 test_apps if ta not in settings.INSTALLED_APPS)
         apps.set_installed_apps(new_installed)
         return super().setup_databases(**kwargs)
 }}}

-- 
Ticket URL: <https://code.djangoproject.com/ticket/7835#comment:42>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

-- 
You received this message because you are subscribed to the Google Groups 
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-updates/066.f05a18a6c40243029e4d27019fdde71b%40djangoproject.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to