IvanAK
IvanAK

Reputation: 213

Python assertEqual error "object has no attribute '_type_equality_funcs'"

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.

Upvotes: 0

Views: 1207

Answers (1)

IvanAK
IvanAK

Reputation: 213

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.

Upvotes: 2

Related Questions