Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/307.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何在一个位置将一个值更改为另一个值,并在两个函数中使用它?_Python_Bdd_Python Behave - Fatal编程技术网

Python 如何在一个位置将一个值更改为另一个值,并在两个函数中使用它?

Python 如何在一个位置将一个值更改为另一个值,并在两个函数中使用它?,python,bdd,python-behave,Python,Bdd,Python Behave,我正在BDD behave中为API编写测试自动化。我需要一个环境切换器。是否有可能在一个位置更改一个值,而不将该值添加到每个函数?例如: 我试图通过增加每个功能的价值来实现这一点,但它使所有项目变得非常复杂 headers = { 'Content-Type': 'application/json', 'country': 'fi' } 仅在标题中切换国家/地区值,例如从“fi”切换到“es” 然后所有功能都应该切换到es环境,例如 def sending_post_requ

我正在BDD behave中为API编写测试自动化。我需要一个环境切换器。是否有可能在一个位置更改一个值,而不将该值添加到每个函数?例如:

我试图通过增加每个功能的价值来实现这一点,但它使所有项目变得非常复杂

headers = {
    'Content-Type': 'application/json',
    'country': 'fi'
}
仅在标题中切换国家/地区值,例如从“fi”切换到“es” 然后所有功能都应该切换到es环境,例如

def sending_post_request(endpoint, user):
    url = fi_api_endpoints.api_endpoints_list.get(endpoint)
    personalId = {'personalId': user}
    json_post = requests.post(url,
                            headers=headers,
                            data=json.dumps(personalId)
                                )
    endpoint_message = json_post.text
    server_status = json_post.status_code

def phone_number(phone_number_status):
    if phone_number_status == 'wrong':
        cursor = functions_concerning_SQL_conection.choosen_db('fi_sql_identity')
        cursor.execute("SELECT TOP 1 PersonalId from Registrations where PhoneNumber is NULL")
        result = cursor.fetchone()
        user_with_no_phone_number = result[0]
        return user_with_no_phone_number
    else:
        cursor = functions_concerning_SQL_conection.choosen_db('fi_sql_identity')
        cursor.execute("SELECT TOP 1 PersonalId from Registrations where PhoneNumber is not NULL")
        result = cursor.fetchone()
        user_with_phone_number = result[0]
        return user_with_phone_number
当我要在标题中从“fi”更改为“es”时:

fi_sql_identity change to es_sql_identity
url = fi_api_endpoints.api_endpoints_list.get(endpoint) change to 
url = es_api_endpoints.api_endpoints_list.get(endpoint)


thx和请帮助

关于您的原始问题,本案例的解决方案是:

def(x):
def长_计算(y):
返回x*y
返回长度计算
#创建不同的函数,无需多次分派
g=f(val_1)
h=f(val_2)
g(val_3)
h(val_3)

问题是你为什么什么都不做?通过更新,您可以将功能简化为:

def phone_number(phone_number_status, db_name='fi_sql_identity'):
    cursor = functions_concerning_SQL_conection.choosen_db(db_name)

    if phone_number_status == 'wrong':
        sql = "SELECT TOP 1 PersonalId from Registrations where PhoneNumber is NULL"
    else:
        sql = "SELECT TOP 1 PersonalId from Registrations where PhoneNumber is not NULL"

    cursor.execute(sql)
    result = cursor.fetchone()
    return result[0]
另外,请不要这样写:

# WRONG
fi_db_conn.send_data()
但请使用一个参数:


并使用配置文件来存储您的端点相对于您的区域,例如考虑:

然后在Python中使用它们:

导入yaml
将open('config.yml')作为f:
配置=yaml.安全荷载(f)
地区='fi'
db_name=config[region]['db_name']#“fi_sql_identity”
#状态=。。。
结果=电话号码(状态、数据库名称)

有关使用YAML的更多信息,请参见。

首先,提供一个封装如何通过提供带有region参数的封装来访问某个区域的资源。作为一个整体提供此功能也是一个好主意

案例1:区域参数需要在功能/场景之间变化

例如,这意味着场景1需要region=“fi”,场景2需要region=“es”。 使用带有区域参数的夹具和夹具标记

在这种情况下,您需要为每个区域编写自己的场景(糟糕的测试重用) 或者使用ScenarioOutline作为模板,让behave为您生成测试(例如使用带有区域参数值的fixture标记)

案例2:对于所有功能/场景(在测试运行期间),区域参数都是恒定的。

通过使用userdata参数,可以支持具有不同区域参数的多个测试运行。 看看行为概念。 这允许您运行
behave-D region=fi…
behave-D region=es…

这个案例提供了更好的testsuite重用,这意味着testsuite的很大一部分是应用于所有区域的公共testsuite


提示:您的代码示例太具体(“基于fi”),这是一种不好的味道。

您知道您的代码有语法错误,对吗?如果您展示(部分)您试图改进的代码,并询问如何改进的问题,您将使问题变得更好。相反,您共享的代码与一个问题不起作用,这个问题不清楚您真正想要解决的问题。这不是我的代码<“es”中的code>value\u 1意味着我要使用“es”环境中的
value\u 1
,我不会使用这样的代码。这只是为了更好地理解problem@Bartol获得有用答案的基本规则是使代码最小化并可运行。但是现在没有人知道你到底想做什么,而且你的代码语法不正确。@knh190代码是粘贴的。我希望现在更清楚,我有一个api端点使用数据库中的数据。要在不同的环境上测试它,我只需要更改标题中的国家。但是,当我改变国家时,我必须使用特定的国家SQL数据库,并且必须将两个DICT从另一个国家更改为其他国家。谢谢您尝试回答我,但这个答案在我的情况下是不正确的。谢谢您的回答,但我不能在pycharm社区中使用yaml。我必须找到另一种方法。@Bartol一个Python dict也可以。其思想不是硬编码,而是使用参数初始化连接。
region = 'fi' # or "es"
db_conn = initialize_conn(region)
db_conn.send_data()
# config.yml
es:
  db_name: es_sql_identity
fi:
  db_name: fi_sql_identity