0

目前我正在用 Python Lettuce 编写一些集成测试。我想为一些 XML 的比较创建一个通用步骤。

我知道在步骤定义之后的 .feature 文件中

Scenario: I want to compare XML's...
 ...
 And I check generated transaction "XYZ" contents with parameters:
 ...

我可以添加 step.hashes,这将在步骤定义中的 step.hashes 属性中可用(some_step_definitions.py 波纹管)

@step(u'And I check generated transaction "([^"]+)" contents with parameters:$')
def check_generated_content(step, transaction_type):
    *(some xml mappings for comparision, appending dictionaries to step.hashes)*
    for item in mappings:
       step.hashes.append({
          'key1': 'val1',
          'key2': 'val2'
       })

我的问题是:是否可以在步骤代码(check_generated_content)中附加标题(key1,key2),当执行测试时,我在测试报告中有标题,例如:

Scenario: I want to compare XML's...
 ...
 And I check generated transaction "XYZ" contents with parameters:
| key1 | key2 |
| val1 | val2 |
...

当我不指定时

And I check generated transaction "XYZ" contents with parameters:
| key1 | val1 |

在功能文件中,我得到了测试报告:

And I check generated transaction "XYZ" contents with parameters:
| | |
| | |

总之..我只想写

And I check generated transaction "XYZ" contents with parameters:

代替

And I check generated transaction "XYZ" contents with parameters:
| key1 | val1 |

每次。

4

1 回答 1

0

所以在写这篇文章的同时,我想出了解决方案。这很容易......在步骤代码定义中,我需要添加的是步骤键的默认值

step.keys = [ 'key1', 'val1' ]

然后我可以将散列附加到 step.hashes。之后我用behaviour_as方法执行了一步

step.behave_as(step.sentence + '\n' + step.represent_hashes())

一切都精美地打印在测试输出中。

于 2015-03-04T08:01:38.233 回答