【问题标题】:Why isn't Django creating blank a database during test?为什么 Django 在测试期间不创建空白数据库?
【发布时间】:2015-03-04 20:08:12
【问题描述】:

当我运行单元测试时,Django 1.6 似乎没有创建一个空白数据库来进行测试,我不明白为什么。 Django docs 说 Django 不使用您的生产数据库,而是创建一个单独的空白数据库进行测试。但是,当我调试测试“test_get_user_ids”并运行命令“UserProxy.objects.all()”时,我会看到生产数据库中的所有用户。现在我知道这个特定的测试会失败,因为我没有将每个 UserProxy 实例保存到数据库中,因此没有生成要测试的 id。但事实仍然是,当我查询 UserProxy 时,我仍然可以看到我的生产数据库中的所有用户,我希望这些用户是空的。为什么会这样?

顺便说一句,我正在使用鼻子测试运行测试:“nosetests -s apps.profile.tests.model_tests.py:UserProxyUT”

谢谢。

# settings.py
DATABASES = {
    'default': {
        # Enable PostGIS extensions
        'ENGINE'  : 'django.contrib.gis.db.backends.postgis',
        'NAME'    : 'myapp',
        'USER'    : 'myappuser',
        'PASSWORD': 'myapppw',
        'HOST'    : 'localhost',
        'PORT'    : '',
    }
}

# apps/profile/models.py
from django.contrib.auth.models import User

class UserProxy(User):
    """Proxy for the auth.models User class."""
    class Meta:
        proxy = True

    @staticmethod
        def get_user_ids(usernames):
            """Return the user ID of each username in a list."""
            user_ids = []
            for name in usernames:
                try:
                    u = User.objects.get(username__exact=name)
                    user_ids.append(u.id)
                except ObjectDoesNotExist:
                    logger.error("We were unable to find '%s' in a list of usernames." % name)
            return user_ids

# apps/profile/tests/model_tests.py
from django.contrib.auth.models import User
from django.test import TestCase
from apps.profile.models import UserProxy

class UserProxyUT(TestCase):
    def test_get_user_ids(self):
        debug()   
        # UserProxy.objects.all() shows usernames from my production database!
        u1 = UserProxy(username='user1')
        u2 = UserProxy(username='user2')
        u3 = UserProxy(username='user3')
        usernames = [u1, u2, u3]
        expected = [u1.id, u2.id, u3.id]
        actual = UserProxy.get_user_ids(usernames)
        self.assertEqual(expected, actual)

【问题讨论】:

  • 您确定没有将生产数据库数据存储为夹具吗?
  • 同时显示DATABASES设置值。谢谢。
  • @alecxe,很抱歉。已显示。
  • @MikeDeSimone,不,我从不使用灯具。
  • 您是否使用不同的设置文件进行测试?

标签: django django-models django-testing


【解决方案1】:

我要试一试,说这是因为您使用的是nosetests 而不是 Django 测试运行器。因为你使用的是nosetests,所以没有调用Django的setup_test_environment,这意味着代码不知道正确使用测试数据库。

以下是 Django 文档中应该提供帮助的相关部分:

Finding data from your production database when running tests?

如果您的代码在编译模块时尝试访问数据库,这将在设置测试数据库之前发生,可能会导致意外结果。例如,如果您在模块级代码中有一个数据库查询并且存在一个真实的数据库,那么生产数据可能会污染您的测试。无论如何,在您的代码中包含此类导入时数据库查询是一个坏主意 - 重写您的代码,使其不会这样做。

还有:

Running tests outside the test runner

如果您想在 ./manage.py test 之外运行测试(例如,从 shell 提示符),您需要先设置测试环境。 Django 提供了一个方便的方法来做到这一点:

>>> from django.test.utils import setup_test_environment
>>> setup_test_environment()

【讨论】:

  • 注意这里要使用django_nose包。
  • Finding data from your production database when running tests? 链接已损坏
猜你喜欢
  • 2015-01-28
  • 2020-07-14
  • 1970-01-01
  • 2019-10-04
  • 1970-01-01
  • 2011-06-15
  • 2021-11-19
  • 1970-01-01
  • 2020-11-17
相关资源
最近更新 更多