如何在Python(PyTest)中传递存储在py文件中作为Json请求体一部分的随机id

2024-09-30 06:26:36 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在探索pytest以自动化测试活动。我想将var值存储在一个.py文件中,该文件位于“TestCases”文件夹之外。JSON正文是:

req_json= "{ \r\n\"ReqParamList\": { \r\n  \"StdValue\": \"age@name\" \r\n } \r\n}"
resp = requests.request("POST", cURL, data=req_json, headers=headers, params=eid)

年龄和姓名从数据库中提取,并作为var存储在单独的py文件中

import mysql.connector
from mysql.connector import errors
try:
    cnx = mysql.connector.connect(
        host="hostname",
        port=port,
        database="dbname",
        user="uname",
        connection_timeout=5,
        password="pw")
# Fname
    FnameCur = cnx.cursor()
    RandomFname = ("select fname from firstnames limit 1")
    FnameCur .execute(RandomFname)
    for row in FnameCur :
        fname = (row[0])
    print("fname is:", fname)
# Lname
    LnameCur = cnx.cursor()
    RandomLname = ("select Lname from lasttnames where Lname in (select "
                   "Lname from (select Lname from lasttnames ORDER BY RAND("
                   ") limit 1)t)")
    LnameCur.execute(RandomLname )
    for row in LnameCur:
       Lname = (row[0])
    print ("Lname is:",Lname)
except Error as e:
    print("Error", e)

finally:
    if (cnx.is_connected()):

        fnameCur .close()
        LnameCur.close()

谁能帮我一下吗


Tags: 文件infromconnectorisvarmysqlselect

热门问题