我的第一次单元测试,我做错了什么?
问题描述:
这是我第一次尝试编写测试,我猜测我自己写了一些测试本身。我的第一次单元测试,我做错了什么?
这里是我的测试:
from django.test import TestCase
from accounts.forms import UserReview
class MyTests(TestCase):
def test_forms(self):
form_data = {'headline': 'test', 'body_text': 'description of item Im selling', 'author: ben'}
form = SellForm(data=form_data)
self.assertEqual(form.is_valid(), True)
我收到以下错误:
ImportError: Failed to import test module: accounts.tests
Traceback (most recent call last):
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/unittest/loader.py", line 254, in _find_tests
module = self._get_module_from_name(name)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/unittest/loader.py", line 232, in _get_module_from_name
__import__(name)
File "/Users/benjamino/Desktop/myproject/myproject/accounts/tests.py", line 8
form_data = {'headline': 'test', 'body_text': 'description of item Im selling', 'author: ben'}
^
SyntaxError: invalid syntax
----------------------------------------------------------------------
Ran 1 test in 0.000s
FAILED (errors=1)
Destroying test database for alias 'default'...
为什么accounts.tests无法导入?上面的代码位于我的accounts/tests.py中。
答
唯一的例外是在哪里的错误是显而易见的:
File "/Users/benjamino/Desktop/myproject/myproject/accounts/tests.py", line 8 form_data = {'headline': 'test', 'body_text': 'description of item Im selling', 'author: ben'} ^ SyntaxError: invalid syntax
拿在form_data
更好看:{“标题”: 'test','body_text':'item im selling'的描述,'作者:ben'}(你在字典中包含一个字符串而不是名称:值对)
答
这是一个简单的语法错误,因为消息告诉你。你的报价是错在你的字典的最后一个元素:'author: ben'
应该'author': 'ben'
我也刚刚意识到这在技术上不是单元测试。 – stephan 2014-10-20 08:27:32