unittest在导入时失败 - 缺少符号

时间:2014-12-12 22:50:56

标签: python pydev

Python,Python命令行和PyDev的新手。 Fedora 20,Python 2.7.5,因为它出现在这个发行版上。我无法编写单元测试。尝试过导入unittest和unittest2(如此处所示)。

from unittest2 import TestCase, main

def IsOdd( n ):
    return n % 2 == 1

class IsOddTests( TestCase ):
    def testOne( self ):
        self.failUnless( IsOdd( 1 ) )
    def testTwo( self ):
        self.failIf( IsOdd( 2 ) )

def main():
    main()

if __name__ == '__main__':
    main()

每次导入都有问题。无论是从Eclipse中的PyDev控制台还是命令行shell,都会发生这种情况。我查看了/usr/lib/python2.7/site-packages/*下的内容,并找到了缺少的符号(TestCase和unit),但这对现在的案例没有帮助。

/usr/bin/python 2.7.5 (default, Nov  3 2014, 14:26:24) 
[GCC 4.8.3 20140911 (Red Hat 4.8.3-7)]
PyDev console: starting.
>>> from unittest2 import TestCase, main
Traceback (most recent call last):
  File "<input>", line 1, in <module>
  File "/usr/lib/python2.7/site-packages/unittest2/__init__.py", line 40, in <module>
    from unittest2.collector import collector
  File "/usr/lib/python2.7/site-packages/unittest2/collector.py", line 3, in <module>
    from unittest2.loader import defaultTestLoader
  File "/usr/lib/python2.7/site-packages/unittest2/loader.py", line 8, in <module>
    import unittest
  File "/home/russ/dev/python-workspace/max/src/unittest.py", line 11, in <module>
    from unittest2 import TestCase, main
  File "/usr/lib/python2.7/site-packages/unittest2/main.py", line 7, in <module>
    from unittest2 import loader, runner
  File "/usr/lib/python2.7/site-packages/unittest2/runner.py", line 7, in <module>
    from unittest2 import result
  File "/usr/lib/python2.7/site-packages/unittest2/result.py", line 26, in <module>
    class TestResult(unittest.TestResult):
AttributeError: 'module' object has no attribute 'TestResult'
>>> from unittest import TestCase, main
Traceback (most recent call last):
  File "<input>", line 1, in <module>
  File "/home/russ/dev/python-workspace/max/src/unittest.py", line 11, in <module>
    from unittest2 import TestCase, main
  File "/usr/lib/python2.7/site-packages/unittest2/__init__.py", line 40, in <module>
    from unittest2.collector import collector
  File "/usr/lib/python2.7/site-packages/unittest2/collector.py", line 3, in <module>
    from unittest2.loader import defaultTestLoader
  File "/usr/lib/python2.7/site-packages/unittest2/loader.py", line 12, in <module>
    from unittest2 import case, suite
  File "/usr/lib/python2.7/site-packages/unittest2/case.py", line 10, in <module>
    from unittest2 import result
  File "/usr/lib/python2.7/site-packages/unittest2/result.py", line 9, in <module>
    from unittest2 import util
ImportError: cannot import name util

2 个答案:

答案 0 :(得分:1)

我从未使用过unittest2。看起来使用普通unittest可能对你的情况有点好一点,因为它内置了python并且经过了很好的审查。我能够让这个为我工作

from unittest import TestCase, main as unittest_main

def IsOdd( n ):
    return n % 2 == 1

class IsOddTests( TestCase ):
    def testOne( self ):
        self.failUnless( IsOdd( 1 ) )
    def testTwo( self ):
        self.failIf( IsOdd( 2 ) )

def main():
    unittest_main()

if __name__ == '__main__':
    main()

然后运行它

0 ✓ greg@MinasArnor ~/workspace $ python test.py 
..
----------------------------------------------------------------------
Ran 2 tests in 0.001s

OK

答案 1 :(得分:1)

我在你的背影中注意到了这一点:

File "/home/russ/dev/python-workspace/max/src/unittest.py", line 11, in <module>

和此:

class TestResult(unittest.TestResult):
AttributeError: 'module' object has no attribute 'TestResult'

重命名您的文件。您已将脚本命名为unittest.py,因此Python正在查看类的您的单元测试脚本(如TestResult),而不是像它应该的内置单元测试模块。