AttributeError:TestSwitch实例没有属性'assertTrue' [英] AttributeError: TestSwitch instance has no attribute 'assertTrue'

查看:26
本文介绍了AttributeError:TestSwitch实例没有属性'assertTrue'的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个下面的pyunit测试用例代码,我在其中收集函数的结果(True或False),并使用它来驱动我的断言。但是,我收到了assertTrue的"无属性"错误。这里缺少什么?

我使用的是python2.7.8和pyunit版本的pyUnit-1.4.1-py2.7。

同样的代码在我的Mac上从Eclipse(pydev插件)运行时,运行得很好。只有当我把它带到我的Linux机器上时,它才抛出下面的错误。因此,在我看来,这是一些包不兼容的问题。

import json
import unittest

class TestSwitch(unittest.TestCase):

    def testFunction(self):
        self.assertTrue(True, "test case failed")

下面是测试套件类。

import unittest
from mysample import TestSwitch

# Create an instance of each test case.
testCase = TestSwitch('testFunction')

# Add test cases to the test suite.
testSuite = unittest.TestSuite()
testSuite.addTest(testCase)

# Execute the test suite.
testRunner = unittest.TextTestRunner(verbosity=2)
testRunner.run(testSuite)

它在错误下面抛出。

bash-3.2$ python mysuite.py
testFunction (mysample.TestSwitch) ... ERROR

======================================================================
ERROR: testFunction (mysample.TestSwitch)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "workspace/pyunit/mysample.py", line 7, in testFunction
    self.assertTrue(True, "test case failed")
AttributeError: TestSwitch instance has no attribute 'assertTrue'
----------------------------------------------------------------------
Ran 1 tests in 0.000s

FAILED (errors=1)
bash-3.2$    

推荐答案

目前,我已经找到了解决此问题的方法,方法是使用‘AssertEquity’与布尔值进行比较,它是有效的。我不确定为什么‘assertTrue’和‘assertFalse’会有问题。我没有更改任何包版本或任何东西。

解决方法代码如下。

 17     def testFunction(self):
 18         res = True
 19         self.assertEqual(res, True, 'test case failed')

这篇关于AttributeError:TestSwitch实例没有属性'assertTrue'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆