简体   繁体   English

BDD表现Python需要创建一个世界地图来保存值

[英]BDD behave Python need to create a World map to hold values

I'm not too familiar with Python but I have setup a BDD framework using Python behave, I now want to create a World map class that holds data and is retrievable throughout all scenarios. 我不太熟悉Python但是我已经使用Python行为设置了BDD框架,我现在想要创建一个包含数据的世界地图类,并且可以在所有场景中检索。

For instance I will have a world class where I can use: 例如,我将有一个世界级,我可以使用:

World w 

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

In one scenario and in another I can then use: 在一个场景和另一个场景中,我可以使用:

World w

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

Edit: 编辑:

Or if there is a built in way of using context or similar in behave where the attributes are saved and retrievable throughout all scenarios that would be good. 或者,如果存在使用上下文或类似行为的内置方式,则可以保存属性,并且可以在所有方案中检索这些属性。

Like lettuce where you can use world http://lettuce.it/tutorial/simple.html 像生菜一样,你可以使用世界http://lettuce.it/tutorial/simple.html

I've tried this between scenarios but it doesn't seem to be picking it up 我在场景之间尝试过这种方法,但它似乎并没有捡到它

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

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

Setting the item in one step in scenario A: w. 在方案A:w中一步设置项目。 setitem ('key', response) setitem ('key',响应)

Getting the item in another step in scenario B: w. 在方案B的另一个步骤中获取该项目:w。 getitem ('key',) getitem ('key',)

This shows me an error though: 这显示了一个错误:

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'

It appears that the World does not hold values here between steps that are run. 似乎世界并没有在运行的步骤之间保留值。

Edit: 编辑:

I'm unsure how to use environment.py but can see it has a way of running code before the steps. 我不确定如何使用environment.py,但可以看到它有一种在步骤之前运行代码的方法。 How can I allow my call to a soap client within environment.py to be called and then pass this to a particular step? 如何调用environment.py中的soap客户端调用,然后将其传递给特定步骤?

Edit: 编辑:

I have made the request in environment.py and hardcoded the values, how can I pass variables to environment.py and back? 我在environment.py中发出了请求并对值进行了硬编码,如何将变量传递给environment.py并返回?

It's called "context" in the python-behave jargon. 它在python行为术语中被称为“上下文”。 The first argument of your step definition function is an instance of the behave.runner.Context class, in which you can store your world instance. 步骤定义函数的第一个参数是behave.runner.Context类的一个实例,您可以在其中存储您的世界实例。 Please see the appropriate part of the tutorial. 请参阅教程的相应部分。

Have you tried the simple approach, using global var , for instance: 您是否尝试过使用global var的简单方法,例如:

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

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

Guess feature can be accessed from context , for instance: 可以从context访问猜测feature ,例如:

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

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

You are looking for: 您正在寻找:
Class variable : A variable that is shared by all instances of a class. 类变量 :由类的所有实例共享的变量。
Your code in Q uses Class Instance variable . Q中的代码使用Class Instance变量
Read about: python_classes_objects 阅读: python_classes_objects

For instance: 例如:

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

Output : 输出

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

Tested with Python:3.4.2 用Python测试:3.4.2
Come back and Flag your Question as answered if this is working for you or comment why not. 如果这对您有用,请回来并将您的问题标记为已回答,或者为什么不回复。

Defining global var in before_all hook did not work for me. 在before_all钩子中定义global var变量对我来说不起作用。 As mentioned by @stovfl 如@stovfl所述

But defining global var within one of my steps worked out. 但是在我的一个步骤中定义了全局变量。

Instead, as Szabo Peter mentioned use the context. 相反,正如Szabo Peter提到的那样使用上下文。

context.your_variable_name = api.response and just use context.your_variable_name anywhere the value is to be used. context.your_variable_name = api.response ,只需在要使用该值的任何位置使用context.your_variable_name

For this I actually used a config file [config.py] I then added the variables in there and retrieved them using getattr. 为此,我实际上使用了配置文件[config.py]然后我在那里添加了变量并使用getattr检索它们。 See below: 见下文:

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

Then retrieved them like: 然后检索它们像:

import config

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM