PyDev中的PyUnit错误

时间:2013-07-21 02:43:43

标签: python pydev python-unittest

我第一次尝试在PyDev中使用PyUnit。我创建了一个unittest.py模块。当我run as -> Python unit-test时,我收到以下错误:

Finding files... done.
Importing test modules ... done.

======================================================================
Traceback (most recent call last):
  File "/Applications/eclipse/plugins/org.python.pydev_2.7.5.2013052819/pysrc/runfiles.py", line 163, in 
    main()
  File "/Applications/eclipse/plugins/org.python.pydev_2.7.5.2013052819/pysrc/runfiles.py", line 77, in main
    pydev_runfiles.main(configuration)
  File "/Applications/eclipse/plugins/org.python.pydev_2.7.5.2013052819/pysrc/pydev_runfiles.py", line 761, in main
    PydevTestRunner(configuration).run_tests()
  File "/Applications/eclipse/plugins/org.python.pydev_2.7.5.2013052819/pysrc/pydev_runfiles.py", line 747, in run_tests
    runner.run(test_suite)
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/unittest/runner.py", line 158, in run
    result.printErrors()
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/unittest/runner.py", line 108, in printErrors
    self.printErrorList('ERROR', self.errors)
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/unittest/runner.py", line 114, in printErrorList
    self.stream.writeln("%s: %s" % (flavour,self.getDescription(test)))
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/unittest/runner.py", line 46, in getDescription
    return '\n'.join((str(test), doc_first_line))
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/unittest/case.py", line 1060, in __str__
    self._testFunc.__name__)
AttributeError: 'str' object has no attribute '__name__'

我的单元测试只是PyDev生成的默认值:

import unittest


class Test(unittest.TestCase):


    def setUp(self):
        pass


    def tearDown(self):
        pass


    def testName(self):
        pass
        print "hello test"


if __name__ == "__main__":
    #import sys;sys.argv = ['', 'Test.testName']
    unittest.main()

我希望它能打印hello test。我错过了什么?

1 个答案:

答案 0 :(得分:4)

好的,我在几分钟内遇到类似的问题后意识到了你的问题。您需要重命名您的文件。将其命名为unittest.py以外的任何名称。这会导致编译器在您的模块和要导入的unittest模块之间混淆。

其次,即使更改了文件名,也可能会遇到同样的错误。这是由于没有从项目中删除unittest.pyc文件引起的。重命名文件,删除以前生成的unittest.pyc文件,测试运行得很好。