有时,您需要动态定义值(例如现在的日期时间、随机字符串、随机整数、文件内容等)并在不同的步骤中使用它们,而无需显式或硬编码值。
所以,我的问题是如何在步骤中定义变量(正确的方法)以在以下步骤中使用这些变量。
一些例子
Given A random string of length "100" as "my_text"
And I log in to my platform
And I ask to add the following post:
| title | description |
| Some example of title | {{my_text}} |
When I submit the post form
Then The posts table shows these posts:
| title | description |
| Some example of title | {{my_text}} |
And I delete any post containing in the description "{{my_text}}"
这是一个基本示例,试图解释为什么我想在步骤中定义变量并将它们保存在上下文中以在以下步骤中使用它。
我的想法是修改 before_step 和 after_step 方法...在上下文中设置一个变量来存储我的自定义变量,如下所示:
def before_step(context):
if not hasattr(context, 'vars'):
context.vars = {}
if hasattr(context, table) and context.table:
parse_table(context)
def parse_table(context):
# Here use a regex to check each cell and look for `"{{<identifier>}}"` and if match, replace the cell value by context.vars[identifier] so the step "the posts table shows these posts will never know what is `{{my_text}}` it will be abstract seeing the random string.
场景大纲,使用类似这样的定义变量"<some_identifier>"
,然后为每个示例替换步骤中的值。
它基本上是为了重现行为,但对于任何类型的步骤,简单或使用表格。
这是做这样的事情的正确方法吗?