首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >assertEqual错误“对象没有属性'_type_equality_funcs'”

assertEqual错误“对象没有属性'_type_equality_funcs'”
EN

Stack Overflow用户
提问于 2021-03-11 17:31:50
回答 1查看 498关注 0票数 0

我不知道为什么我会有以下问题?

守则:

代码语言:javascript
复制
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))

错误四得到:

代码语言:javascript
复制
AttributeError: 'TestIncrementDictionaryValues' object has no attribute '_type_equality_funcs'

但是,如果我从其中删除"init“方法,并将字典放在"TestIncrementDictionaryValues”方法中,那么一切都正常。

EN

回答 1

Stack Overflow用户

发布于 2021-03-11 20:39:34

最后,在谷歌和阅读之后,这是我的工作或修复。

代码语言:javascript
复制
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))

这是从here处理的

self.assertEqual只适用于继承unittest.TestCase类的类,而Utility没有这样做。我建议尝试将您的实用程序方法放在BaseTestCase类下。给它一个不以test_开头的名称,稍后调用这个新函数来验证您对许多其他函数的断言。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/66587574

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档