Reputation: 18643
Am new to Python, Python command line and PyDev. Fedora 20, Python 2.7.5 as it came on this distro. I am unable to write a unit test. Have tried importing unittest and unittest2 (as here).
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()
Each import has its problems. This happens whether from PyDev console in Eclipse or at the command line shell. I have looked at what's under /usr/lib/python2.7/site-packages/* and I find the missing symbols (TestCase and unit), but this isn't helpful to present case.
/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
Upvotes: 1
Views: 719
Reputation: 5107
I noticed this in your back trace:
File "/home/russ/dev/python-workspace/max/src/unittest.py", line 11, in <module>
and this:
class TestResult(unittest.TestResult):
AttributeError: 'module' object has no attribute 'TestResult'
Rename your file. You've named your script unittest.py
, so Python is looking at your unittest script for classes (like TestResult) and not the builtin unittest module like it's supposed to.
Upvotes: 1
Reputation: 5588
I've never used unittest2. It seems like using plain unittest might be a bit better for your situation given that its a python builtin and is well vetted. I was able to get this to work for me
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()
Then running it
0 ✓ greg@MinasArnor ~/workspace $ python test.py
..
----------------------------------------------------------------------
Ran 2 tests in 0.001s
OK
Upvotes: 1