PyD中的PyUnit错误

2024-09-27 07:20:05 发布

您现在位置:Python中文网/ 问答频道 /正文

我第一次尝试在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生成的默认值:

^{pr2}$

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


Tags: runinpytestlinelibraryframeworkunittest
1条回答
网友
1楼 · 发布于 2024-09-27 07:20:05

好吧,我意识到你的问题,在经历了几分钟类似的问题之后。你需要重命名你的文件。把它命名为unittest.py. 这会导致编译器混淆您的模块和您要导入的unittest模块。在

其次,即使更改了文件名,也可能会遇到相同的错误。这是由单元测试.pyc未从项目中删除文件。重命名文件,删除以前生成的单元测试.pyc文件,测试应该可以正常运行。在

相关问题 更多 >

    热门问题