Python Unittest: AttributeError: no attribute 'assertTrue' when running on Linux
Python Unittest: AttributeError: no attribute 'assertTrue' when running on Linux
我 运行在 Python
中进行了一些非常简单的单元测试,发现 assertTrue()
函数不起作用,而在同一个测试用例中 assertEqual()
工作正常。
为了简化问题,我将代码最小化为以下内容:
import unittest
class easyTest (unittest.TestCase):
def setUp(self):
pass
def test_true(self):
self.assertTrue(True)
if __name__ == "__main__":
unittest.main()
这批代码 运行 在我的 Windows 笔记本电脑上完美无缺,但是 returns
AttributeError: easyTest instance has no attribute 'assertTrue'
当我在 Linux 上尝试 运行 时。
在两台笔记本电脑上,我都在使用 python 2.7.6
,在 IDE pyCharm Community Edition 2017.1.4
上。我的 Linux 笔记本电脑是 运行ning Ubuntu 14.04.1
我在这里发现了一个非常相似的问题:
好像没人回答这个问题,我又来这里问了,希望能得到一些突出的答案。
您的 python 路径中是否有第二个单元测试模块或包?
如果您创建了 unittest.py 文件或包含 __init__.py
文件的 unittest 目录,python 可以在找到标准 python 中的普通模块之前找到它图书馆。
命名本地模块或包unittest等同于命名本地变量list或dict或map;您正在使用本地重新定义来掩盖内置名称。
将该模块或包重命名为其他名称以解决此问题。
根据您的评论,您使用的单元测试版本是(长期弃用的)独立 PyUnit 1.4.1
包。作为 package's homepage mentions:
Unless you're stuck in the year 2000, PyUnit is in your Python standard library as module unittest.
事实上,unittest
已添加到 Python 2.1 的标准库中。
IOW,除非你坚持使用过时的遗留代码库(使用 Python < 2.1!),否则你应该卸载 PyUnit,你的问题就会得到解决。
我 运行在 Python
中进行了一些非常简单的单元测试,发现 assertTrue()
函数不起作用,而在同一个测试用例中 assertEqual()
工作正常。
为了简化问题,我将代码最小化为以下内容:
import unittest
class easyTest (unittest.TestCase):
def setUp(self):
pass
def test_true(self):
self.assertTrue(True)
if __name__ == "__main__":
unittest.main()
这批代码 运行 在我的 Windows 笔记本电脑上完美无缺,但是 returns
AttributeError: easyTest instance has no attribute 'assertTrue'
当我在 Linux 上尝试 运行 时。
在两台笔记本电脑上,我都在使用 python 2.7.6
,在 IDE pyCharm Community Edition 2017.1.4
上。我的 Linux 笔记本电脑是 运行ning Ubuntu 14.04.1
我在这里发现了一个非常相似的问题:
您的 python 路径中是否有第二个单元测试模块或包?
如果您创建了 unittest.py 文件或包含 __init__.py
文件的 unittest 目录,python 可以在找到标准 python 中的普通模块之前找到它图书馆。
命名本地模块或包unittest等同于命名本地变量list或dict或map;您正在使用本地重新定义来掩盖内置名称。
将该模块或包重命名为其他名称以解决此问题。
根据您的评论,您使用的单元测试版本是(长期弃用的)独立 PyUnit 1.4.1
包。作为 package's homepage mentions:
Unless you're stuck in the year 2000, PyUnit is in your Python standard library as module unittest.
事实上,unittest
已添加到 Python 2.1 的标准库中。
IOW,除非你坚持使用过时的遗留代码库(使用 Python < 2.1!),否则你应该卸载 PyUnit,你的问题就会得到解决。