我不知道为什么我会有以下问题?
守则:
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))错误四得到:
AttributeError: 'TestIncrementDictionaryValues' object has no attribute '_type_equality_funcs'但是,如果我从其中删除"init“方法,并将字典放在"TestIncrementDictionaryValues”方法中,那么一切都正常。
发布于 2021-03-11 20:39:34
最后,在谷歌和阅读之后,这是我的工作或修复。
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_开头的名称,稍后调用这个新函数来验证您对许多其他函数的断言。
https://stackoverflow.com/questions/66587574
复制相似问题