【问题标题】:How do I skip a test in the behave python BDD framework?如何跳过行为 python BDD 框架中的测试?
【发布时间】:2016-04-07 16:45:24
【问题描述】:

我正在处理几个月前部分完成的代码分支,其中包含相互交织的依赖项。因此,向前推进的最简单方法是将特定分支上的失败测试标记为待处理(rspec 方式)或跳过,并在所有内容合并后处理它们。

在其最终报告中,behave 报告了通过的测试数、失败的测试数、跳过的测试数和未测试的测试数(当我按 Ctrl-C 中止运行时,这些测试非零)。所以behave 是跳过测试的概念。我如何能 访问那个?

【问题讨论】:

    标签: bdd python-behave


    【解决方案1】:

    如果您想在命令行中控制事情,那么您可以按照 Tymoteusz Paul 在另一个答案中的建议进行操作。简而言之,您可以使用任何想要标记功能和场景的标签,然后使用--tags 根据您使用的标签选择或取消选择功能和场景。该文档给出了使用@slow 标记慢速场景然后使用behave --tags=slow 仅运行慢速测试或使用behave --tags=-slow 排除慢速测试的示例。建议阅读documentation 以了解--tags 允许的语法。

    通过上述方法,您可以使用@skip 并使用behave --tags=-skip。这将排除标有@skip 的所有内容,但每次调用都必须包含额外的参数是很烦人的。 @skip 不能自己告诉 Behave 跳过,而不需要命令行上的任何参数吗?

    如果您想要一个 @skip 标记来跳过用它标记的功能和场景,而不需要额外的参数,那么,从 Behave 1.2.5 开始,您必须构建功能到您的environment.py 文件中。what this answer 建议的相反,它不是内置的。我们添加这样的功能:

    def before_feature(context, feature):
        if "skip" in feature.tags:
            feature.skip("Marked with @skip")
            return
    
        # Whatever other things you might want to do in this hook go here.
    
    def before_scenario(context, scenario):
        if "skip" in scenario.effective_tags:
            scenario.skip("Marked with @skip")
            return
    
        # Whatever other things you might want to do in this hook go here.
    

    .skip 方法的参数是跳过的原因。

    我总是使用.effective_tags 在场景中进行标签测试。 .effective_tags 字段继承在功能上设置的标签。在手头的情况下,这没有什么区别,因为如果该功能具有@skip,那么它中的场景将被强制跳过。但是,我更喜欢坚持场景中的标签检查应该使用.effective_tags 以便标签继承工作的一般原则。


    等等! tutorial 不是说@skip 是内置的吗?

    不,本教程的介绍有点误导,因为它提供了一个列表,在 @wip 旁边显示 @skip@wip 是内置的,所以 @skip 也是内置的,对吗?不,它们在“预定义或常用标签”列表中。 @skip 只是“经常使用”。它经常被使用,因为以一种信息丰富的方式使用“跳过”这个词来将某些东西标记为跳过。这并不意味着标签是内置在 Behave 中的。

    【讨论】:

    • 这里稍微改进一下,将 and not context.config.tags.check(["skip"]) 添加到 if 语句中,这样显式的 --tags=skip 仍然可以让您运行所有当前跳过的测试。
    • 对我前面的更正:所需的添加是and not (context.config.tags and context.config.tags.check(["skip"])),因为一个空标签表达式会为每个标签报告True
    • 行为文档已移动。新的教程链接(指向标签)是behave.readthedocs.io/en/stable/…
    • @Louis:感谢您添加此内容,这真的很有帮助,对于您在没有命令的情况下跳过的问题可以从环境中控制,我在下面添加了我的解决方案来解决这个问题。
    【解决方案2】:

    Behave 不会跳过测试,它会跳过已经失败的场景中的步骤。不直接支持跳过测试,但如果您只需要运行部分测试,则可以control the execution with tags

    【讨论】:

      【解决方案3】:

      如何在 Python Behave 中跳过场景及其应用示例:

      所有函数都必须在 environment.py 模块中声明:

      环境.py

      在 before_all() 中声明 skip_scenario = True

      添加一个skip_scenarios=True来控制跳过各种特征文件中的特定场景。控制器在environment.pybefore_all函数中设置>**

      def before_all(context):
          pass
      
          context.scenario_metadata_dict = {}
          context.feature_metadata = {'skip_scenario': True}
      

      在 before_scenario() 中添加条件:

      如果特征文件中的任何场景包含标签“environmentSkip”并且skip_scenario = True则只跳过那些场景。

      def before_scenario(context, scenario):
          if context.feature_metadata['skip_scenario'] and "environmentSkip" in scenario.effective_tags:
              scenario.skip("Marked with skip from environment Control")
              return
          elif "skip" in scenario.effective_tags:
              scenario.skip("Marked with @skip tag in feature File")
              return
      

      功能文件:

      如果您希望在特定场景中跳过标记,则将具有标记 @environmentSkip 标记。

        @system-testing @regression @environmentSkip
        Scenario: validate  Global Exclusion Rules for PrimaryEarlyLifeCLI Strategy
         When  derive the expected Results for global Exclusion rule NEGESTAT
            | KEY             | VALUE                                                                                              |
            |test_scenario_id |TS1_GlobalExclusion_NEGESTAT|
      

      注意:上面的示例处理了跳过场景,处理跳过的自然方式和受控处理跳过。

      应用程序

      自然方式:将帮助开发人员跳过用于调试目的的场景。

      受控方式:将有助于控制特定环境的场景。

      如果需要,也可以为 before_feature() 添加相同的控件。

      【讨论】:

        【解决方案4】:

        老问题,新答案。

        Behave 目前支持过滤掉标签,如下例所示,其中执行所有未使用 @my_tag_name 标记的测试:

        behave --tags=~@my_tag_name path/to/my_tests.feature
        

        来源:来自行为帮助

        # behave --tags-help
        Scenarios inherit tags declared on the Feature level. The simplest
        TAG_EXPRESSION is simply a tag::
        
            --tags @dev
        
        You may even leave off the "@" - behave doesn't mind.
        
        When a tag in a tag expression starts with a ~, this represents boolean NOT::
        
            --tags ~@dev
        
        A tag expression can have several tags separated by a comma, which represents
        logical OR::
        
            --tags @dev,@wip
        
        The --tags option can be specified several times, and this represents logical
        AND, for instance this represents the boolean expression
        "(@foo or not @bar) and @zap"::
        
            --tags @foo,~@bar --tags @zap.
        
        Beware that if you want to use several negative tags to exclude several tags
        you have to use logical AND::
        
            --tags ~@fixme --tags ~@buggy.
        

        【讨论】:

          【解决方案5】:

          您可以将预定义的“@skip”标签用于您想要跳过的场景或功能,并且行为会自动跳过测试场景或整个功能。

          【讨论】:

          • 不真实。我刚刚尝试将@skip 添加到我的一个场景中,但正如我预期的那样,它被 Behave 1.2.5 忽略了。 @wip 是预定义的,正如 here 所指出的那样。但是,我在任何地方都没有提到@skip
          • 抱歉,skip 仅适用于场景。因此,您可以放置​​ @skip 标签来跳过一个场景,但不能跳过整个功能。模式详情:jenisys.github.io/behave.example/tutorials/tutorial11.html
          • 教程说“预定义或经常使用的标签”。 @skip@slow 不是“预定义的”。它们是“经常使用的”。换句话说,您可以随意使用@skip 标签,然后您可以使用--tags 选项控制它,就像任何其他标签,这就是accepted answer盖子。您可以使用名为@skip@potato@asdf 的标签来跳过场景。 @skip 更容易理解,但没有特殊地位。
          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2021-01-11
          • 2014-12-09
          相关资源
          最近更新 更多