【发布时间】:2019-11-10 00:01:37
【问题描述】:
我目前正在为 python 脚本编写一些测试。该脚本完全按照我的预期运行,没有任何问题。 我遇到的问题是,当我从单元测试中调用一个函数时,我收到一条:IndexError: list index out of range 消息。
对于下面显示的单元测试,我遵循this 示例。
我无法通过谷歌搜索找到类似的问题,所以我什至不知道从哪里开始。如果我从脚本打印项目,它会打印我期望的值。
def build_message(self, {some other into}):
# Get email templates
template = Templates.objects.filter(template_id=1)
# This next line is what gives me the error.
fields = template[0]
我在这样的单元测试中调用它:
def test_send_email(self):
# Mock 'smtplib.SMTP' class
with patch("smtplib.SMTP") as mock_smtp:
# Build test message
to_address = "email@address
# Build message
msg = handle_command.build_message(self, {some other info})
我得到的错误是这样的。我不明白为什么这里说索引超出范围,运行我的脚本时效果很好。
fields = template[0]
File "/usr/local/lib/python3.5/dist-packages/django/db/models/query.py", line 307, in __getitem__
return qs._result_cache[0]
IndexError: list index out of range
【问题讨论】:
标签: python