Mark
Mark

Reputation: 2031

AttributeError: 'module' object has no attribute 'TestCase'

I have file with unittest named: test.py

My code:

import unittest

class Test(unittest.TestCase):

    def myTest(self):
        a = 1
        self.assertEqual(a, 1)


if __name__ == '__main__':
    unittest.main()

When I press F5, I get an error:

Traceback (most recent call last):
  File "/home/mariusz/Pulpit/test.py", line 1, in <module>
    import unittest
  File "/home/mariusz/Pulpit/unittest.py", line 3, in <module>
AttributeError: 'module' object has no attribute 'TestCase'

Upvotes: 34

Views: 45678

Answers (3)

John Prawyn
John Prawyn

Reputation: 1663

In my case, one of the dependency was not there.

import os
from some_package import some_module

the some_module was not there in python(python couldn't import it). Once I commented the import statement python started discovering my test cases.

python -m unittest tests.test_my_own_module

Upvotes: 0

Ranjita Shetty
Ranjita Shetty

Reputation: 625

Your script named unittest.py is replacing the module file. Rename your unittest.py script to something else.

Upvotes: 2

Martijn Pieters
Martijn Pieters

Reputation: 1121644

You have a local file named unittest.py that is being imported instead:

/home/mariusz/Pulpit/unittest.py

Rename that file or remove it altogether. Make sure you remove any corresponding unittest.pyc file in the same folder if it is there.

The file is masking the standard library package.

Upvotes: 79

Related Questions