8

我安装了 django-nose 1.0 作为 Django 1.3.1 项目的测试运行程序。我正在遵循pypi 页面上有关仅测试模型的说明。

这是我的 settings.py testrunner 配置:

TEST_RUNNER = 'django_nose.NoseTestSuiteRunner'

我已经使用这个 testrunner 运行了几个月的测试没有问题。现在我正在尝试测试一个抽象类,并且我正在使用仅测试模型,但是我编写的特定测试会引发错误。

根据文档,我只需要将测试类包含在测试期间导入的文件之一中。我将测试放在“测试”文件夹中,并分解为几个较小的测试文件。这是我的测试/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”的表)不存在。

关于为什么只测试模型没有加载的任何想法?

4

2 回答 2

0

是的,似乎这仍然是一个问题。用 django==1.6 和 django-nose==1.3 看到它

一种解决方法是将所有模型放入__init__.py您的tests/文件夹中

GitHub 上的相关问题:django-nose/issues/77

于 2015-01-07T19:44:58.803 回答
0

您需要在测试数据库中创建模型,为此您需要手动生成迁移或在数据库中创建表。您可以检查我对第二个变体的实现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]
于 2017-08-30T15:50:15.473 回答