我有以下单元测试问题,我试图通过。Python对象测试
def test_map2(self):
self.home = []
self.home.append(Bed('Bedroom'))
self.home.append(Sofa('Living Room'))
self.home.append(Table('Bedroom'))
mapping = map_the_home(self.home)
self.assertTrue(isinstance(mapping['Bedroom'][0], Bed))
self.assertTrue(isinstance(mapping['Living Room'][0], Sofa))
self.assertTrue(isinstance(mapping['Bedroom'][1], Table))
每个值都应该是一个列表,里面有一个或多个Furnishing子类实例。
这是我目前的尝试。
class Furnishing(object):
def __init__(self, room):
self.room = room
class Sofa(Furnishing):
name = 'Sofa'
class Bed(Furnishing):
name = 'Bed'
class Table(Furnishing):
name = 'Table'
def map_the_home(home):
results = {}
for furnitiure in home:
if furnitiure.room in results:
results[furnitiure.room] = (results[furnitiure.room],furnitiure)
else:
results[furnitiure.room] = furnitiure
return results
def counter(home):
counter_list = {}
for line in home:
if line.name in counter_list:
print(line.room,line.name)
counter_list[line.name] = counter_list[line.name] + 1
else:
counter_list[line.name] = 1
for furniture in counter_list:
print('{0} = {1}'.format(furniture,counter_list[furniture]))
if __name__ == "__main__":
home = []
home.append(Bed('Bedroom'))
home.append(Sofa('Living Room'))
home.append(Table('Bedroom'))
map_the_home(home)
counter(home)
该计数器只是另一部分,但希望给出完整的代码。我以为我有这个使用字典,但作为测试说我需要在每个值中的家具子类实例内的列表。任何有识之士将是巨大的
'结果[家具空间] =(结果[家具空间],家具)'不符合你的要求。 – Blckknght