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

南笙酒味 提交于 2019-12-05 12:52:38

问题


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'

回答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.



来源:https://stackoverflow.com/questions/26893504/attributeerror-module-object-has-no-attribute-testcase

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!