Performing specific steps once before a script outline - Python Behave

As the name implies, I want to follow some specific configuration / environment setup steps before the script outline. I know what exists for scripts Background, but Behave splits the script scheme into several scripts and thus triggers the background for each input in the script loop.

This is not what I want. For some reason I can’t provide the code I'm working with, however I will write an example function 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 will happen in this case: Behave will power cycle and check the USB connection for each output Hi, No, Yesthat will lead to three cycles of food and three checks connection

, Behave , .

?

+4
2

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.

< > /enviroment.py

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

/enviroment.py

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

. Background, Feature. , Scenario s.

, 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
0

Source: https://habr.com/ru/post/1618841/


All Articles