gpt4 book ai didi

python - 无法运行 Django 单元测试。返回 django.core.exceptions.ImproperlyConfigured

转载 作者:太空宇宙 更新时间:2023-11-03 14:44:55 25 4
gpt4 key购买 nike

我多年来一直在我的 Django 应用程序上运行单元测试 (test.py),没有出现任何问题。在我的最后一次 session 中,我正在修改我的 MYSQL 数据库和模型。在试验模型时,我一直在删除并重新创建数据库,删除迁移文件并从头开始进行迁移。

现在,单元测试将无法运行。

Traceback (most recent call last):
File "/opt/pycharm-2017.2.1/helpers/pycharm/_jb_unittest_runner.py", line 35, in <module>
main(argv=args, module=None, testRunner=unittestpy.TeamcityTestRunner, buffer=not JB_DISABLE_BUFFERING)
File "/usr/lib/python3.5/unittest/main.py", line 93, in __init__
self.parseArgs(argv)
File "/usr/lib/python3.5/unittest/main.py", line 140, in parseArgs
self.createTests()
File "/usr/lib/python3.5/unittest/main.py", line 147, in createTests
self.module)
File "/usr/lib/python3.5/unittest/loader.py", line 219, in loadTestsFromNames
suites = [self.loadTestsFromName(name, module) for name in names]
File "/usr/lib/python3.5/unittest/loader.py", line 219, in <listcomp>
suites = [self.loadTestsFromName(name, module) for name in names]
File "/usr/lib/python3.5/unittest/loader.py", line 153, in loadTestsFromName
module = __import__(module_name)
File "/home/danny/PycharmProjects/AskArby/deals/tests.py", line 3, in <module>
from deals.models import Retailer
File "/home/danny/PycharmProjects/AskArby/deals/models.py", line 5, in <module>
class Retailer(models.Model):
File "/home/danny/PycharmProjects/AskArby/deals/models.py", line 6, in Retailer
name = models.CharField(max_length=200)
File "/home/danny/.virtualenvs/AskArby/lib/python3.5/site-packages/django/db/models/fields/__init__.py", line 1061, in __init__
super(CharField, self).__init__(*args, **kwargs)
File "/home/danny/.virtualenvs/AskArby/lib/python3.5/site-packages/django/db/models/fields/__init__.py", line 172, in __init__
self.db_tablespace = db_tablespace or settings.DEFAULT_INDEX_TABLESPACE
File "/home/danny/.virtualenvs/AskArby/lib/python3.5/site-packages/django/conf/__init__.py", line 56, in __getattr__
self._setup(name)
File "/home/danny/.virtualenvs/AskArby/lib/python3.5/site-packages/django/conf/__init__.py", line 39, in _setup
% (desc, ENVIRONMENT_VARIABLE))
django.core.exceptions.ImproperlyConfigured: Requested setting DEFAULT_INDEX_TABLESPACE, but settings are not configured. You must either define the environment variable DJANGO_SETTINGS_MODULE or call settings.configure() before accessing settings.

PyCharm 还告诉我没有找到测试。

这是 models.py 的开头:

from django.db import models
from django.utils.translation import ugettext_lazy as _

class Retailer(models.Model):
name = models.CharField(max_length=200)
base_url = models.URLField(max_length=500)
currency = models.CharField(max_length=3)
#css parameters for finding deals and subfields in HTML
deal_container_css = models.CharField(max_length=300)
title_css = models.CharField(max_length=300)
price_css = models.CharField(max_length=300)
old_price_css = models.CharField(max_length=300, null=True, blank=True)
brand_css = models.CharField(max_length=300, null=True, blank=True)
image_css = models.CharField(max_length=300, null=True, blank=True)
description_css = models.CharField(max_length=300, null=True, blank=True)
#for things that look like deal containers but aren't
exclude_css = models.CharField(max_length=300, null=True, blank=True)
shipping_css = models.CharField(max_length=300, null=True, blank=True)
product_model_css = models.CharField(max_length=300, null=True, blank=True)

测试.py:

from django.test import TestCase
import unittest
from deals.models import Retailer

class RetailerTests(TestCase):
fixtures = ['initial_data.json']

class TestRetailer(unittest.TestCase):

def setUp(self):
self.bestbuy = Retailer(Retailer.objects.get(pk=1))

def test_name(self):
self.assertEqual(self.bestbuy.name, 'Best Buy')

我正在使用 Pycharm,将 runserver 作为 manage.py 命令运行。我尝试从命令行设置 DJANGO_SETTINGS_MODULE,它回显了正确的答案:AskArby.settings。我尝试调用 settings.configure(),它告诉我设置已配置。

此外,当我运行 runserver 时,我收到此警告:

?: (1_8.W001) The standalone TEMPLATE_* settings were deprecated in Django 1.8 and the TEMPLATES dictionary takes precedence. You must put the values of the following settings into your default TEMPLATES dict: TEMPLATE_DIRS.
deals.Marketplace.listing: (fields.W340) null has no effect on ManyToManyField.

为什么我的应用程序似乎无法运行 test.py 中的单元测试?

最佳答案

如果您从 ide 运行而不是从命令行运行,请在运行方法之前添加此代码:

import os
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "[your appname].settings")
import django
django.setup()

祝你好运!

关于python - 无法运行 Django 单元测试。返回 django.core.exceptions.ImproperlyConfigured,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46312076/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com