1

I'm trying to use Python's unittest module to compare results and expected values and get test statistics in the end. However, below sample code gives me the following error:

File "...", line 16, in <module>
  run_tests.test_equal()
File "...", line 13, in test_equal
  self.assertEqual(self.result, self.expected)
File "/usr/lib/python2.7/unittest/case.py", line 512, in assertEqual
  assertion_func = self._getAssertEqualityFunc(first, second)
File "/usr/lib/python2.7/unittest/case.py", line 493, in _getAssertEqualityFunc
  asserter = self._type_equality_funcs.get(type(first))
AttributeError: 'RunTests' object has no attribute '_type_equality_funcs'

Sample code:

import unittest

class RunTests(unittest.TestCase):
   def __init__(self, result, expected):
      self.result = result
      self.expected = expected

   def runTest(self):
      test_equal(self)

   def test_equal(self):
      self.assertEqual(self.result, self.expected)

run_tests = RunTests(9, 9)
run_tests.test_equal()

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

How can I use the module's comparison functions on varying input values? I have found this post, but the problem appears to be different.

Thanks a lot in advance.

0

2 Answers 2

4

You skipped the call of __init__ of a base class inside your RunTests.__init__, which takes care of defining _type_equality_funcs.

def __init__(self, result, expected):
      super().__init__()
      self.result = result
      self.expected = expected
Sign up to request clarification or add additional context in comments.

4 Comments

If I make the change you suggest I get the error "TypeError: super() takes at least 1 argument (0 given)" Googling it tells me that this is a Python 3 functionality, but I am using Python 2.7.12, and I cannot install any new modules/upgrade the Python version.
C'mon. super(subClass, instance).method(args) In your case: super(RunTests, self).__init__()
Sorry for the noob-question, but I haven't used Python and OO programming in ages. Now all I get is "TypeError: __init__() takes exactly 3 arguments (2 given) " -.-
unittest framework expects that your class that inherit from TestCase` have a single-argument __init__ method which accepts methodName. So you should follow this interface. So you have to pass values somehow differently
2

you need to call __init__ method of base class. this might be useful :

    import unittest
    class RunTests(unittest.TestCase):
       def __init__(self, *args, **kwargs):
          super(RunTests, self).__init__()

       def runTest(self):
          self.test_equal()

       def test_equal(self):
          self.assertEqual(9, 9)

    run_tests = RunTests()
    run_tests.test_equal()

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.