I'm trying to implement my Django (1.11.7, Python 3.6) unit testing in Visual Studio. When I run them in my terminal (python manage.py test) they run fine, while when executing from the VS Test Explorer it fails with an error saying:
django.core.exceptions.AppRegistryNotReady: Apps aren't loaded yet.
I've read that newer Django versions require an explicit django.setup() to be called, so I've tried to add it both to setUpClass(cls) and to setUpTestData(cls).
import django
from datamodel.models import Engagement, BusinessUnit
from django.contrib.auth.models import User
from django.test import TestCase, Client
class TestViewRPA(TestCase):
@classmethod
def setUpClass(cls):
super(TestViewRPA, cls).setUpClass()
django.setup()
@classmethod
def setUpTestData(cls):
super(TestViewRPA, cls).setUpTestData()
django.setup()
User.objects.create_user('dummyuser')
eng = Engagement.objects.create(name = 'Test Client', owner=User.objects.get(username='dummyuser'))
businessunit = BusinessUnit.objects.create(name = 'Business Unit', engagement=eng)
def test_get_industry_segment1(self):
businessunit = BusinessUnit.objects.get(name = 'Business Unit')
c = Client()
response = c.get('/rpa/%d' % businessunit.id)
self.assertContains(response, 'id":1,"businessunit_set":[{"id":1,', status_code=200)
Result from executing from CLI
...>env\Scripts\python.exe manage.py test app
Creating test database for alias 'default'...
System check identified no issues (0 silenced).
----------------------------------------------------------------------
Ran 3 tests in 0.028s
OK
Destroying test database for alias 'default'...
Any ideas? Thanks in advance
You are getting this error because you are importing from before 之前从导入
You have already imported django so delete
from django.contrib.auth.models import User
and instead of
User.objects.create_user('dummyuser')
use django.contrib.auth.models like this
django.contrib.auth.models.User.objects.create_user('dummyuser')
or
if you want to keep everything simple just add django.setup() statement before importing anything from django.contrib.auth.models
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.