跳过步骤实现中的行为步骤

Gia*_*nio 9 python python-behave

有没有办法告诉步骤实现中的行为跳过当前步骤?

就像是:

@given("bla bla bla")
def step():
    skip_current_step()
Run Code Online (Sandbox Code Playgroud)

用例是我想检查是否安装了一些其他软件.如果没有,我希望跳过完整的场景.

m4t*_*4tx 10

让我改进@ Barry的回答:

基本上,他提出的建议(即scenario.mark_skipped())等于:

scenario.skip(require_not_executed=True)
Run Code Online (Sandbox Code Playgroud)

确切地说,mark_skipped()源代码如下:

def mark_skipped(self):
    """Marks this scenario (and all its steps) as skipped.
    Note that this method can be called before the scenario is executed.
    """
    self.skip(require_not_executed=True)
    assert self.status == "skipped", "OOPS: scenario.status=%s" % self.status
Run Code Online (Sandbox Code Playgroud)

skip() 定义如下:

def skip(self, reason=None, require_not_executed=False)
Run Code Online (Sandbox Code Playgroud)

一些东西:

  • require_not_executed=True表示如果任何步骤已经过去,则不能跳过场景,即mark_skipped()在第二步或后续步骤中将抛出异常,然后跳过所有步骤,而不是仅仅跳过其他步骤
  • skip()允许提供跳过的原因,然后将其记录为WARN.

此外,scenario对象在上下文中可用context.scenario(旁边context.feature).

最终,简单的例子:

@given("test photos in upload directory")
def step_impl(context):
    if not connection_available():
        context.scenario.skip(reason='internet connection is unavailable')
Run Code Online (Sandbox Code Playgroud)

结果:

Skipped: skipped
WARNING:behave:SKIP Scenario Retrieving uploaded photo details: internet connection is unavailable
Run Code Online (Sandbox Code Playgroud)

  • 为了那些仍然在1.2.4上的人的利益(就像我在几分钟前一样):来自这个答案中引用的行为代码库的代码来自1.2.5. (2认同)

Bar*_*rry 5

我不知道您是否可以从步骤内部跳过,但您可以在功能级别跳过整个场景:

def before_feature(context, feature):
    is_software_installed = some_foo_magic()

    if not is_software_installed:
        for scenario in feature.scenarios:
            if depends_on_software(scenario):
                scenario.mark_skipped()
Run Code Online (Sandbox Code Playgroud)

Feature, Scenario, 和ScenarioOutline都有一个mark_skipped()方法。