Python AttributeError: 'module' 对象没有属性 'TestCase'

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26893504/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 01:07:31  来源:igfitidea点击:

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

pythonunit-testingassertions

提问by Mark

I have file with unittest named: test.py

我有单元测试的文件: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:

当我按 F5 时,出现错误:

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'

采纳答案by Martijn Pieters

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

您有一个名为的本地文件unittest.py正在导入:

/home/mariusz/Pulpit/unittest.py

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

重命名该文件或将其完全删除。确保删除unittest.pyc同一文件夹中的任何相应文件(如果存在)。

The file is masking the standard library package.

该文件掩盖了标准库包。

回答by Ranjita Shetty

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

您名为 unittest.py 的脚本正在替换模块文件。将您的 unittest.py 脚本重命名为其他名称。