【问题标题】:Django test setup not being used未使用 Django 测试设置
【发布时间】:2018-02-03 04:34:31
【问题描述】:

我正在为一个项目使用 Django cookiecutter 1.11。 尝试为模型编写一些基本测试。但是在测试用例中没有使用设置方法。

from django.test import TestCase
from myapp.users.models import User
from ..models import Book

class ModelTests(TestCase):
    def setup(self):
        self.username = 'john'
        self.password = '123'
        self.user = User.objects.create(name=self.username,
                                        password=self.password
                                       )

    def test_create_book(self):
        Book.objects.create(artist=self.user,
                            title=“An Art Book“,
                            category=“Art”,
                            )
        self.assertEquals(Book.objects.all().count(), 1)

运行 manage.py test 后我收到此错误消息

Book.objects.create(artist=self.user,
AttributeError: 'ModelTests' object has no attribute 'user'

但是当我将设置中的行放入测试用例时,它会起作用。 我错过了什么吗?

【问题讨论】:

标签: django


【解决方案1】:

该方法应称为setUp,而不是setup

【讨论】:

    猜你喜欢
    • 2017-04-06
    • 2017-10-31
    • 2018-03-12
    • 2011-11-04
    • 2012-03-14
    • 1970-01-01
    • 2016-08-08
    • 2013-02-04
    • 2018-06-11
    相关资源
    最近更新 更多