AttributeError: TestSwitch instance has no attribute 'assertTrue'
I have the following pyunit script code where I collect the result of a function (True or False) and use it for my assertion. However, I am getting a "no attribute" error for assertTrue. What's missing here?
I am using python 2.7.8 and pyunit version PyUnit-1.4.1-py2.7.
The same code when run from Eclipse (pydev plugin) from my Mac works fine. Only when I take this into my Linux box it throws below error. So to me it looks like a package incompatibility issue.
import json
import unittest
class TestSwitch(unittest.TestCase):
def testFunction(self):
self.assertTrue(True, "test case failed")
Below is the test case class.
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)
It throws below error.
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$
source to share
Now I have solved a workaround for this problem using 'assertEqual', comparing against boolean and it works. I'm not sure why "assertTrue" and "assertFalse" is having problems in this regard. I haven't changed any version of the package or anything else.
The workaround code looks like this.
17 def testFunction(self):
18 res = True
19 self.assertEqual(res, True, 'test case failed')
source to share