Running certain steps once before a scenario outline - Python Behave
Asked Answered
B

4

11

As the title suggests, I wish to run some certain configuration / environment setup steps before a scenario outline. I know there is Background to do this for scenarios, but Behave splits a scenario outline into multiple scenarios and thus runs the background for every input in the scenario outline.

This is not what I want. For certain reasons I cannot provide the code I am working with however I will write up an example feature file.

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      |

What would happen in this case is Behave would power cycle and check USB connection for each output Hi, No, Yes resulting in three power cycles and three connection checks

What I want is for Behave to power cycle once and check the connection once and then run all three tests.

How would I go about doing this?

Barth answered 7/12, 2015 at 4:31 Comment(0)
I
8

Your best bet is probably to use the before_feature environment hook and either a) tags on the feature and/or b) the feature name directly.

For example:

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
        ''')

alternate steps/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
        ''')
Isomagnetic answered 2/12, 2016 at 2:18 Comment(0)
M
0

I'm facing exactly the same problem. There is an expensive Background, which should be performed only once per Feature. What is actually needed to solve that problem, is the ability to store state between Scenarios.

My solution for this problem was to use behave.runner.Context#_root which is kept throughout the whole run. I know what accessing private members is not a good practice - I will be very happy to learn cleaner way.

# 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
Morphophoneme answered 18/1, 2016 at 7:56 Comment(1)
I believe TomDotDom's answer is the correct approach. You can do it this way, but this is what the steps/enviroment.py is intended for, and in comparison, this way feels a little "hacky".Broadloom
E
0

You Can do this :

  1. Create environment.py in Features folder

  2. Inside environment.py : from behave import fixture

  3. Write Code :

from behave import fixture

def before_feature(context, feature): print("Runs Before Each Feature")

def after_feature(context, feature): print("Run After Each Feature")

def before_scenario(context, scenario): print("Run Before Each Scenario")

def after_scenario(context, scenario): print("Run After Each Scenario")

#Now run your Tc : Your O/P will be Runs Before Each Feature Run Before Each Scenario Run After Each Scenario Run After Each Feature

Ebon answered 20/7, 2020 at 19:38 Comment(1)
Why would you mention import fixture in this example? And no, neither before_feature nor before_scenario give you control over scenario outlines, which the OP was asking for.Yama
M
0

You should not use Background. You should use Scenario. If you use Background, Background steps repeat. But use Scenario keywords, steps can not repeat.

Feature: Example
Scenario: Scenario for Background
  When Login Step
  Then Verify Login Successfully
 Scenario Outline: Example
   When I read the arduino
    Then I get some <'output'>

Example: Outputs
| 'output' |
| Hi       |
| No       |
| Yes      |
Multivalent answered 20/8, 2022 at 16:30 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.