【问题标题】:Running certain steps once before a scenario outline - Python Behave在场景大纲之前运行某些步骤 - Python Behave
【发布时间】:2015-12-07 04:31:11
【问题描述】:

正如标题所示,我希望在场景大纲之前运行一些特定的配置/环境设置步骤。我知道有 Background 可以为场景执行此操作,但是 Behave 将场景大纲拆分为多个场景,因此为场景大纲中的每个输入运行背景。

这不是我想要的。由于某些原因,我无法提供我正在使用的代码,但是我将编写一个示例功能文件。

Background: Power up module and connect
Given the module is powered up
And I have a valid USB connection

Scenario Outline: Example
    When I read the arduino
    Then I get some <'output'>

Example: Outputs
| 'output' |
| Hi       |
| No       |
| Yes      |

在这种情况下会发生什么情况,Behave 将重启电源并检查每个输出 HiNoYes 的 USB 连接,从而导致三个电源重启和三个连接检查

我想要的是让 Behave 重启一次并检查一次连接,然后运行所有三个测试。

我该怎么做呢?

【问题讨论】:

    标签: python bdd scenarios python-behave


    【解决方案1】:

    您最好的选择可能是使用 before_feature 环境钩子和 a) 功能上的标签和/或 b) 直接的功能名称。

    例如:

    some.feature

    @expensive_setup
    Feature: some name
      description
      further description
    
      Background: some requirement of this test
        Given some setup condition that runs before each scenario
          And some other setup action
    
      Scenario: some scenario
          Given some condition
           When some action is taken
           Then some result is expected.
    
      Scenario: some other scenario
          Given some other condition
           When some action is taken
           Then some other result is expected.
    

    steps/environment.py

    def before_feature(context, feature):
        if 'expensive_setup' in feature.tags:
            context.execute_steps('''
                Given some setup condition that only runs once per feature
                  And some other run once setup action
            ''')
    

    备用步骤/environment.py

    def before_feature(context, feature):
        if feature.name == 'some name':
            context.execute_steps('''
                Given some setup condition that only runs once per feature
                  And some other run once setup action
            ''')
    

    【讨论】:

      【解决方案2】:

      我面临着完全相同的问题。 有一个昂贵的Background,每个Feature 只能执行一次。 解决该问题实际需要的是在Scenarios 之间存储状态的能力。

      我对这个问题的解决方案是使用behave.runner.Context#_root,它会在整个运行过程中保持不变。我知道访问私人成员不是一个好习惯 - 我会很高兴学习更清洁的方式。

      # XXX.feature file
      Background: Expensive setup
        Given we have performed our expensive setup
      
      # steps/XXX.py file
      @given("we have performed our expensive setup")
      def step_impl(context: Context):    
          if not context._root.get('initialized', False):
              # expensive_operaion.do()
              context._root['initialized'] = True
      

      【讨论】:

      • 我相信TomDotDom's answer 是正确的方法。你可以这样做,但这是steps/enviroment.py的目的,相比之下,这种方式感觉有点“hacky”。​​
      【解决方案3】:

      你可以这样做:

      1. 在 Features 文件夹中创建 environment.py

      2. 在 environment.py 内部:从行为导入夹具

      3. 编写代码:

      从行为导入夹具

      def before_feature(上下文,特征): print("在每个功能之前运行")

      def after_feature(上下文,特征): print("在每个特征之后运行")

      def before_scenario(上下文,场景): print("在每个场景之前运行")

      def after_scenario(上下文,场景): print("在每个场景之后运行")

      #现在运行你的 Tc : 你的 O/P 将是 在每个功能之前运行 在每个场景之前运行 在每个场景之后运行 在每个功能之后运行

      【讨论】:

      • 为什么你会在这个例子中提到import fixture?不,before_featurebefore_scenario 都不能让您控制 OP 要求的 场景大纲
      猜你喜欢
      • 2011-04-06
      • 2018-07-25
      • 1970-01-01
      • 1970-01-01
      • 2021-12-06
      • 1970-01-01
      • 2017-03-28
      • 1970-01-01
      • 2016-02-13
      相关资源
      最近更新 更多