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

37,057

Solution 1

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.

Solution 2

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

Share:
37,057

Related videos on Youtube

Mark
Author by

Mark

Updated on March 08, 2021

Comments

  • Mark
    Mark over 3 years

    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'
    
  • user3224237
    user3224237 over 3 years
    Your answer is just a copy of the accepted answer above from more than five years ago.