python - Testing abstract models in Django -
i trying test functionality of abstract base models in django app. have been reading on how these tests , seems need create test model.
so have done so, have crated directory called my_app/tests
, created models.py
in there. have test model uniqueslugtestmodel
.
now guess need call makemigrations
, migrate
on test app. add new test app settings in test , call migrate. reason makemigrations
, migrate
not seeing my_app.tests
app not in settings. why looking @ different settings live settings overriding in tests?
is best way test abstract models in django? how can make management commands @ overridden settings?
from django.conf import settings django.core.management import call_command django.test import testcase django.test import override_settings my_app.tests.models import uniqueslugtestmodel class uniqueslugmodeltestcase(testcase): @override_settings(installed_apps=settings.installed_apps + ['my_app.tests']) def setup(self): print(settings.installed_apps) call_command('makemigrations', 'my_app.tests', interactive=false) call_command('migrate', 'my_app.tests', interactive=false) @override_settings(installed_apps=settings.installed_apps + ['my_app.tests', ]) def teardown(self): call_command('migrate', 'my_app.tests', 'zero', interactive=false) @override_settings(installed_apps=settings.installed_apps + ['my_app.tests', ]) def test_auto_slug_creation(self): c1 = uniqueslugtestmodel(name='company 1') c1.save() c1.refresh_from_db() self.assertequal(c1.slug, 'company-1') @override_settings(installed_apps=settings.installed_apps + ['my_app.tests', ]) def test_unique_slug_generation(self): usm = uniqueslugtestmodel(name='company 1') usm.save() usm.refresh_from_db() usm2 = uniqueslugtestmodel(name='company 1') usm2.save() usm2.refresh_from_db() self.assertnotequal(usm.slug, usm2.slug)
the print(settings.installed_apps)
prints out of installed apps including my_app.tests
app have added in there.
but fails app 'my_app.tests' not found. in installed_apps?
Comments
Post a Comment