BDD behaves in Python to create a world map for storing values

I am not too familiar with Python, but I have a BDD structure using Python, now I want to create a world map class that stores data and is retrieved in all scripts.

For example, I will have a class in which I can use:

World w 

w.key.add('key', api.response)

In one scenario and in another I can use:

World w

key = w.key.get('key'). 

Edit:

Or, if there is a built-in way to use context or similar in maintenance where attributes are stored and retrieved in all scenarios that would be good.

Like a salad, where you can use the world http://lettuce.it/tutorial/simple.html

I tried this between scripts but didn't seem to collect it

class World(dict):
    def __setitem__(self, key, item):
        self.__dict__[key] = item
        print(item)

    def __getitem__(self, key):
        return self.__dict__[key]

A: w. setitem ( "", )

B: w. getitem ('key',)

:

Traceback (most recent call last):
  File "C:\Program Files (x86)\Python\lib\site-packages\behave\model.py", line 1456, in run
    match.run(runner.context)
  File "C:\Program Files (x86)\Python\lib\site-packages\behave\model.py", line 1903, in run
    self.func(context, *args, **kwargs)
  File "steps\get_account.py", line 14, in step_impl
    print(w.__getitem__('appToken'))
  File "C:Project\steps\world.py", line 8, in __getitem__
    return self.__dict__[key]
KeyError: 'key'

, World .

:

, environment.py, , . environment.py , ?

Edit:

environment.py hardcoded, environment.py ?

+4
3

[config.py] getattr. . :

WSDL_URL = 'wsdl'
USERNAME = 'name'
PASSWORD = 'PWD'

:

import config

getattr(config, 'USERNAME ', 'username not found')
-1

"" , python. - behave.runner.Context, . . .

+3

, global var, :

def before_all(context):
    global response
    response = api.response

def before_scenario(context, scenario):
    global response
    w.key.add('key', response)

feature context, :

def before_feature(context, feature):
    feature.response = api.response

def before_scenario(context, scenario):
    w.key.add('key', context.feature.response)

:
: , .
Q .
: python_classes_objects

:

class World(dict):
    __class_var = {}

    def __setitem__(self, key, item):
        World.__class_var[key] = item

    def __getitem__(self, key):
        return World.__class_var[key]

# Scenario A
A = World()
A['key'] = 'test'
print('A[\'key\']=%s' % A['key'] )
del A

# Scenario B
B = World()
print('B[\'key\']=%s' % B['key'] )

:

A['key']=test
B['key']=test  

Python: 3.4.2
, , .

+3
source

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


All Articles