在Behave框架中,可以使用execute_steps方法来模拟场景大纲输出。代码示例如下:
from behave import step, given, when, then, use_step_matcher
from behave.model import Table
use_step_matcher("parse")
@given("I have a list of numbers")
def step_impl(context):
context.numbers = [1, 2, 3, 4, 5]
@when("I add {number:d} to each number in the list")
def step_impl(context, number):
for i in range(len(context.numbers)):
context.numbers[i] += number
# Use execute_steps to mimic scenario outline output
context.execute_steps(f"""
Then the list should be:
| numbers |
| {" | ".join(str(n) for n in context.numbers)} |
""")
@then("the list should be:")
def step_impl(context):
expected = [list(map(int, row)) for row in context.table]
assert context.numbers == expected[0]
在这个例子中,当执行'I add 2 to each number in the list”的步骤时,使用execute_steps方法来模拟输出场景大纲。执行该步骤后,将会执行与'the list should be:”步骤相同的表格验证步骤,以检查预期输出是否与实际输出匹配。
上一篇:Behave枚举
下一篇:Behaveoptional相关