【问题标题】:My first unit test, what am I doing wrong?我的第一个单元测试,我做错了什么?
【发布时间】:2014-10-20 08:25:04
【问题描述】:

这是我第一次尝试编写测试,我猜我在编写测试本身时明显搞砸了。

这是我的测试:

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

【问题讨论】:

  • 我也刚刚意识到这在技术上不是单元测试。

标签: django unit-testing python-2.7


【解决方案1】:

异常在哪里出错了:

文件“/Users/benjamino/Desktop/myproject/myproject/accounts/tests.py”,第 8 行 form_data = {'headline': 'test', 'body_text': '我卖的商品描述', '作者: ben'} ^ SyntaxError:无效的语法

好好看看form_data: {'headline': 'test', 'body_text': 'description of item Im sell', 'author: ben'}(你有在您的字典中包含一个字符串而不是名称:值对)

【讨论】:

    【解决方案2】:

    如消息所示,这是一个简单的语法错误。您的 dict 的最后一个元素中的引用是错误的:'author: ben' 应该是 'author': 'ben'

    【讨论】:

      【解决方案3】:

      从异常本身可以清楚地看出错误在您的语法中。表单数据格式应该是 {name : value} 但在你的情况下它是 {'string : value'} 如果这不能解决你的问题,也许这可以帮助你 Link

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2010-11-29
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-05-14
        相关资源
        最近更新 更多