unit testing - Emulating an app with models in a django unittest -


im writing code retrieves info installed apps, defined models, , stuff based on information, im having problems writing clean, nice unittest. there way emulate or add app in unittests without have run manage.py startproject, manage.py startapp in testsfolder have test app available unittests?

sure, try on size:

from django.conf import settings django.core.management import call_command django.test.testcases import testcase django.db.models import loading  class apptestcase(testcase):     '''     adds apps specified in `self.apps` `installed_apps` ,     performs `syncdb` @ runtime.     '''      apps = ()     _source_installed_apps = ()      def _pre_setup(self):         super(apptestcase, self)._pre_setup()          if self.apps:             self._source_installed_apps = settings.installed_apps             settings.installed_apps = settings.installed_apps + self.apps             loading.cache.loaded = false             call_command('syncdb', verbosity=0)      def _post_teardown(self):         super(apptestcase, self)._post_teardown()          if self._source_installed_apps:             settings.installed_apps = self._source_installed_apps             self._source_installed_apps = ()             loading.cache.loaded = false 

your test case this:

class someapptestcase(apptestcase):      apps = ('someapp',) 

in case wondering why, did override of _pre_setup() , _post_teardown() don't have bother calling super() in setup() , teardown() in final test case. otherwise, pulled out of django's test runner. whipped , worked, although i'm sure that, closer inspection, can further optimize , avoid calling syncdb every time if won't conflict future tests.

edit:

so seem have gotten out of way, thinking need dynamically add new models. if you've created app testing purposes only, here's can have discovered during tests.

in project directory, create test.py file contain test settings. should this:

from settings import *  # registers test app discovery installed_apps += ('path.to.test.app',) 

you can run tests python manage.py test --settings=myproject.test , app in installed apps.


Comments

Popular posts from this blog

Javascript line number mapping -

linux - Mailx and Gmail nss config dir -

c# - Is it possible to remove an existing registration from Autofac container builder? -