python - PyDev 中的 PyUnit 错误

标签 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__'

My unit test is just the PyDev-generated default:

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。我错过了什么?

最佳答案

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

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

关于python - PyDev 中的 PyUnit 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17768566/

相关文章:

java - 安装 pydev 时出错

python - 如何忽略 Pycharm 2017.1 中的某些单元测试?

unit-testing - 如何对 tornado + async def 进行单元测试?

python - python 的新 openCV API 中的 getCentralMoment 去了哪里?

python - 如何在 Flask 上显示我的 IP 地址?

python - 情绪分析数据未显示在 csv 文件中

python - np.where 多个逻辑语句 pandas

python - 调试在 chroot 环境中执行的 python 脚本

python - Eclipse Pydev 未加载外部模块,终端工作正常

python-3.x - 在 python 中模拟 BigQuery 连接