我想在运行一堆测试(在场景大纲中定义)之前只运行一次precondition
。
假设feature file
是这样的:
Background:
Given Fan is powered
#Test to check fan speed
@TEST_FAN-1 @SuperFan
Scenario Outline: Checking fan speed
Given fan is not running
When send speed command with <speed>
Then fan is running with <speed>
Examples:
| speed |
| 5 |
| 50 |
| 100 |
我对Given Fan is powered
的定义如下:
@given("Fan is powered")
def step_impl(context):
assert conetxt.fan.is_powered
此先决条件在场景大纲中定义的每个测试之前执行。有没有办法只运行一次?
一种可能性是在before_all
中添加上下文属性,以跟踪所讨论的步骤之前是否执行过。这仍然会在日志中显示该步骤,但随后的尝试将是no-ops。
def before_all(context):
context.precondition_cache = set()
@given("fan is powered")
def step_impl(context):
if "fan is powered" not in context.precondition_cache:
# power up the fan
context.precondition_cache.add("fan is powered")