Search code examples
pythonlinuxunit-testingassertattributeerror

Python Unittest: AttributeError: no attribute 'assertTrue' when running on Linux


I am running some very simple unit tests in Python, and found that the assertTrue() function won't work, while in the same testcase the assertEqual() is working fine. To simplify the issue, I have minimized the code into the following:

import unittest

class easyTest (unittest.TestCase):
    def setUp(self):
        pass

    def test_true(self):
        self.assertTrue(True)

if __name__ == "__main__":
    unittest.main()

This batch of codes runs perfectly on my Windows laptop, but returns

AttributeError: easyTest instance has no attribute 'assertTrue'

when I try to run it on Linux.

On both laptops, I am using python 2.7.6, on IDE pyCharm Community Edition 2017.1.4. My Linux laptop is running Ubuntu 14.04.1

I have found a very similar question here: AttributeError: TestSwitch instance has no attribute 'assertTrue' And since it seems that nobody is answering the question, I am asking here again, hoping for some prominent answers.


Solution

  • According to your comment, the unittest version you are using is (the long deprecated) stand-alone PyUnit 1.4.1 package. As the package's homepage mentions:

    Unless you're stuck in the year 2000, PyUnit is in your Python standard library as module unittest.

    And indeed, unittest was added to the stdlib in Python 2.1.

    IOW, unless you're stuck with an antediluvian legacy code base (using Python < 2.1 !), you should just uninstall PyUnit and your problem will be solved.