2017-09-28 51 views
0

这是为了测试这个类;然而,我收到一个错误,我不知道如何解决它。Python速成教程,测试你的代码示例

import unittest 
from name_function import get_formatted_name 

class NamesTestCase(unittest.TestCase): 
    """Tests for 'name_function.py'""" 

    def test_first_last_name(self): 
    """Do names like 'Mark James' work?""" 
    formatted_name = get_formatted_name('mark','James') 
    self.assertEqual(formatted_name,'Mark James') 

unittest.main() 

这里是正在测试的类。

def get_formatted_name(first, last): 
    """This is where the name is formatted correctly""" 
    full_name = first + " " + last 
    return full_name.title() 

我得到的错误是这样的:

/Desktop/python_work/test_name_function.py", line 1, in <module> 
    import unittest 
    File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/unittest/__init__.py", line 58, in <module> 
    from .result import TestResult 
    File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/unittest/result.py", line 5, in <module> 
    import traceback 
    File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/traceback.py", line 3, in <module> 
    import linecache 
    File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/linecache.py", line 10, in <module> 
    import tokenize 
    File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/tokenize.py", line 96, in <module> 
    class TokenInfo(collections.namedtuple('TokenInfo', 'type string start end line')): 
AttributeError: 'module' object has no attribute 'namedtuple' 

任何有线索?

+6

没有你的名字你自己的文件或模块'collections'一个? – PRMoureu

+0

'import unittest'发生异常 - 它看起来与您的课​​程没有任何关系。 – wwii

+0

我完全没有碰到unittest文件 –

回答

0

我相信你需要,如果 '' == '主要' 添加: unittest.main()到您的代码。

下面的代码应该修正这个错误 -

import unittest 
from name_function import get_formatted_name 

class NamesTestCase(unittest.TestCase): 
    """Tests for 'name_function.py'""" 

    def test_first_last_name(self): 
     """Do names like 'Mark James' work?""" 
     formatted_name = get_formatted_name('mark','James') 
     self.assertEqual(formatted_name,'Mark James') 
if '__name__' == '__main__': 
    unittest.main() 
+0

这与导入错误有什么关系? –