【问题标题】:Step in Python Lettuce. Is it possible to prepend step.hashes headers in step code?加入 Python 生菜。是否可以在步骤代码中添加 step.hashes 标头?
【发布时间】:2015-03-04 07:56:01
【问题描述】:

目前我正在用 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 |

每次。

【问题讨论】:

  • 好的,我需要回答我的问题,而不是评论它:)

标签: lettuce


【解决方案1】:

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

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

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

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

所有内容都精美地打印在测试输出中。

【讨论】:

  • step.keys = ['key1', 'key2'] 就足够了
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-02-24
  • 1970-01-01
  • 2014-10-07
  • 1970-01-01
  • 2017-11-02
  • 2021-06-07
相关资源
最近更新 更多