0

I can't figure out why I'm having the following issue?

The code:

from unittest import TestCase


def increment_dictionary_values(d, i):
    for k, v in d.items():
        d[k] = v + i
    return d


class TestIncrementDictionaryValues(TestCase):
    def __init__(self):
        self.d = {'a': 1}

    def test_increment_dictionary_values(self, inc_val, test_val):
        dd = increment_dictionary_values(self.d, inc_val)
        self.assertEquals(dd['a'], test_val, msg="Good")


obj1 = TestIncrementDictionaryValues()
print(obj1.test_increment_dictionary_values(1,2))

The error iv got:

AttributeError: 'TestIncrementDictionaryValues' object has no attribute '_type_equality_funcs'

But if I remove the "init" method out of it and put the dictionary in the "TestIncrementDictionaryValues" method, than everything works ok.

2
  • Well, you redefined the __init__() method of TestCase class, and you have no longer access to its attributes. Commented Mar 11, 2021 at 17:45
  • If i add super(TestIncrementDictionaryValues, self).__init__() still i have other error TypeError: assertEqual() missing 1 required positional argument: 'second' Commented Mar 11, 2021 at 17:52

1 Answer 1

2

Finally after some googling and reading this is the work or fix that i have

from unittest import TestCase
'''
The other solution im haveing without any error is to remove the __init__ method and place the 
dictionary (d) inside the "test_increment_dictionary_values" method. And this is the basic one. 
But this way we have Class atribuets d and method atributes. 
'''

def increment_dictionary_values(d, i):
    for k, v in d.items():
        d[k] = v + i
    return d


class TestIncrementDictionaryValues(TestCase):
    def __init__(self,*args, **kwargs):
        super(TestIncrementDictionaryValues, self).__init__()
        self.d = {'a': 1}

    def test_increment_dictionary_values(self, inc_val, test_val):
        dd = increment_dictionary_values(self.d, inc_val)
        self.assertEqual(dd['a'], test_val)


obj1 = TestIncrementDictionaryValues()
print(obj1.test_increment_dictionary_values(1,2))
obj2 = TestIncrementDictionaryValues()
print(obj2.test_increment_dictionary_values(-1,0))

This is coped from here

self.assertEqual will be only available to classes which inherits unittest.TestCase class, which your Utility class not doing. I suggest try putting your Utility methods under BaseTestCase class. Give it a name not starting with test_, later on call this new function to validate your asserts for numerous other functions.

Sign up to request clarification or add additional context in comments.

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.