小编Fed*_*tro的帖子

在行为python中定义上下文变量

有时,您需要动态定义值(如现在的datetime,随机字符串,随机整数,文件内容等),并在不同的步骤中使用它们,而不是显式或硬编码值.

所以,我的问题是如何在步骤中定义变量(正确的方法)在以下步骤中使用这些变量.

一些例子

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}}"
Run Code Online (Sandbox Code Playgroud)

这是一个基本的例子,试图解释为什么我想在步骤中定义变量并将它们保存在上下文中以便在以下步骤中使用它.

我的想法是修改before_step和after_step方法......在上下文中设置一个变量来存储我的自定义变量,如下所示:

def before_step(context): …
Run Code Online (Sandbox Code Playgroud)

python testing contextmanager python-behave

7
推荐指数
1
解决办法
2695
查看次数

标签 统计

contextmanager ×1

python ×1

python-behave ×1

testing ×1