在Visual Studio中使用Django加载单元测试失败
我正在Visual Studio 2015中使用Django 1.10.5。我的项目正在虚拟环境中运行。我正在学习初学者教程。该项目运行良好,但当我尝试从Visual Studio“测试资源管理器”运行单元测试时,它们失败并出现错误:“django.core.exceptions.AppRegistryNotReady:尚未加载应用程序。” 这是我的测试课:在Visual Studio中使用Django加载单元测试失败,django,visual-studio,visual-studio-2015,django-testing,django-tests,Django,Visual Studio,Visual Studio 2015,Django Testing,Django Tests,我正在Visual Studio 2015中使用Django 1.10.5。我的项目正在虚拟环境中运行。我正在学习初学者教程。该项目运行良好,但当我尝试从Visual Studio“测试资源管理器”运行单元测试时,它们失败并出现错误:“django.core.exceptions.AppRegistryNotReady:尚未加载应用程序。” 这是我的测试课: import datetime from django.test import TestCase from django.utils im
import datetime
from django.test import TestCase
from django.utils import timezone
from .models import Question
class QuestionTestCase(TestCase):
def test_wasPublishedRecently_FutureQuestion_FALSE(self):
futureTime = timezone.now() + datetime.timedelta(days=30)
futureQuestion = Question(datePublished=futureTime)
self.assertIs(futureQuestion.wasPublishedRecently(), False)
在Python for Visual Studio中,django(>=1.7)测试在PTV中运行测试时需要显式的setup() 将(setUpClass)代码放在类定义的旁边:
class ViewTest(TestCase):
"""Tests for the application views."""
if django.VERSION[:2] >= (1, 7):
# Django 1.7 requires an explicit setup() when running tests in PTVS
@classmethod
def setUpClass(cls):
super(ViewTest, cls).setUpClass()
django.setup()
def test_home(self):
"""Tests the home page."""
response = self.client.get('/')
self.assertContains(response, 'Home Page', 1, 200)
您还需要将“testserver”添加到settings.py中允许的主机中,我还发现我需要调用
django.setup()。但是,它在@classmethod
中对我不起作用。我正在使用test\u plus
包进行更好的Django测试,该包的测试模块立即提供设置。因此,我需要设置SETTINGS\u模块
env变量,并在test\u plus.test import TestCase的之前调用django.setup()