2014-10-20 252 views
0

這是我第一次嘗試編寫測試,我猜測我自己寫了一些測試本身。我的第一次單元測試,我做錯了什麼?

這裏是我的測試:

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中。

+0

我也剛剛意識到這在技術上不是單元測試。 – stephan 2014-10-20 08:27:32

回答

0

唯一的例外是在哪裏的錯誤是顯而易見的:

 
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'}(你在字典中包含一個字符串而不是名稱:值對)

0

這是一個簡單的語法錯誤,因爲消息告訴你。你的報價是錯在你的字典的最後一個元素:'author: ben'應該'author': 'ben'

0

從異常本身,錯誤是在你的語法。表單數據格式應該是{name:value},但在你的情況下它是{'string:value'},如果這樣不能解決你的問題,也許這可以幫助你 Link

相關問題