我安装了Django -nas1.0作为Django 1.3.1项目的测试运行程序。我正在遵循on the pypi page关于仅限测试的模型的说明。
以下是我的settings.py测试运行程序配置:
TEST_RUNNER = 'django_nose.NoseTestSuiteRunner'
我已经使用这个测试器运行了几个月的测试,没有出现任何问题。现在,我正在尝试测试一个抽象类,并且我正在使用一个仅限测试的模型,但是我编写的特定测试抛出了一个错误。
根据文档,我只需要在测试期间导入的一个文件中包含测试类。我把测试放在一个“test”文件夹中,然后分成几个较小的测试文件。以下是我的tests/model_tests.py (出于工作原因,模型和应用程序被有意重命名):
from django.tests import TestCase
from myapp.models import AbstractFoo
class Foo(AbstractFoo):
pass
class TestFoo(TestCase):
def setUp(self):
self.foo = Foo.objects.create(name="Tester",
description="This is a test", ...)
... [tests follow]我在setUp的第一行收到一个错误:
DatabaseError: relation "tests_foo" does not exist
LINE 1: INSERT INTO "tests_foo" ("name", "description", "display...如果我在测试中放置一个断点并检查数据库,表'tests_foo‘(或名称中包含'foo’的任何表)都不存在。
关于为什么不加载仅限测试的模型,有什么想法吗?
发布于 2015-01-08 03:44:59
是的,看起来这仍然是一个问题。我用django==1.6和django-nose==1.3看到了
一种解决方法是将__init__.py中的所有模型放在tests/文件夹中
GitHub上的相关问题:django-nose/issues/77
发布于 2017-08-30 23:50:15
您需要在测试数据库中创建模型,为此您需要在数据库中手动生成迁移或创建表。您可以查看我的第二个变体https://github.com/erm0l0v/django-fake-model的实现
这段代码应该能像您预期的那样工作:
from django.tests import TestCase
from myapp.models import AbstractFoo
from django_fake_model import models as f
class Foo(f.FakeModel, AbstractFoo):
pass
@Foo.fake_me
class TestFoo(TestCase):
def setUp(self):
self.foo = Foo.objects.create(name="Tester",
description="This is a test", ...)
... [tests follow]https://stackoverflow.com/questions/11730874
复制相似问题