假设我有一个定义如下的步骤:
Then I would expect to see the following distribution for Ford
| engine | doors | color |
| 2.1L | 4 | red |
我有读取表并执行断言的步骤实现,如下所示:
@then('I would expect to see the following distribution for {car_type}')
def step(context, car_type):
car = find_car_method(car_type)
for row in context.table:
for heading in row.headings:
assertEqual(getattr(car, heading),
row[heading],
"%s does not match. " % heading + \
"Found %s" % getattr(car, heading))
(我这样做是因为这种方法允许添加更多字段,但保持其通用性足以用于检查汽车属性的许多用途)。
当我的汽车对象有 4 个门(作为 int)时,它不匹配,因为数据表要求有“4”个门(作为 unicode str)。
我可以实现这种方法来检查列的名称并对不同的字段进行不同的处理,但是当添加一个新字段时,维护变得更加困难,因为还有一个地方可以添加它。我宁愿在步骤数据表中指定它。就像是:
Then I would expect to see the following distribution for Ford
| engine | doors:int | color |
| 2.1L | 4 | red |
有没有类似的东西可以用来实现这一点(因为这不起作用)?
请注意,在某些情况下,我需要从遇到相同问题的数据表中创建。尝试使用“汽车”对象的类型来确定类型是没有用的,因为在这种情况下它是 None 。
谢谢,
拜尔