2016-10-03 63 views
1

我有一个以下pyunit测试用例代码,其中我收集函数的结果(True或False)并使用它来驱动我的断言。但是,我得到assertTrue的“no attribute”错误。这里缺少什么?AttributeError:TestSwitch实例没有属性'assertTrue'

我使用python 2.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$  
+0

您能为您显示测试类的类定义吗? TestSwitch是否继承自TestCase? – elethan

+0

如建议的那样用更完整的代码更新了问题。 – AnilJ

+0

你能在Linux机器上用最小的例子重现问题吗?例如,没有开关的东西,只是简单的'self.assertTrue(1)'或类似的单元测试的最基本的。 – Evert

回答

0

现在我已经用“assertEqual便”与布尔值进行比较想通了这个问题的解决方法和它的作品。我不确定为什么'assertTrue'和这个问题'assertFalse'有问题。我没有更改任何软件包版本或任何东西。

解决方法代码如下。

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