Python Forum

Full Version: PyTest >> Yaml parsed data to create api test request
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I have a YAML file in db. I am pulling yaml and getting the required param to build PyTest requests.
Seems like I am good till getting expected params. Now the problem is, how to pass those params in PyTest code block in the loop?
kind of new to Python.. could anyone please help me with this?

+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
##Pulling data from db where in column YAML is placed##

mycur1 = cnx.cursor()

descselector1 = ("select des from t_uc;")
mycur1.execute(descselector1)
descr1 = mycur1.fetchall() ##Gives us the list of tuples
#print(descr1)

mycur2 = cnx1.cursor()

CSIdselector = ("select * from r_cnt_se_i_map;")
mycur2.execute(CSIdselector)
CSIDmapping =  mycur2.fetchall()
print(CSIDmapping)

##Fetch expected params from YAML##  
class PSLoader(yaml.SafeLoader):
    pass

def constructor(loader, node):
    return loader.construct_mapping(node)

# to handle the custom tag
PSLoader.add_constructor(
    'tag:yaml.org,2002:c.tcs.ucm.model.uCCo.Cig',
    constructor
)

for entry, in desc1:
    parsed = yaml.load(entry, Loader=PSLoader)

    try:
        mt = parsed['requests'][0]['param']['mt']
        num = parsed['requests'][0]['param']['num']
        aMB = parsed['requests'][0]['param']['aMB']
        uc = parsed['uC'][0]['ucId']


        cSId = parsed['requests'][0]['param']['cSId']
        for i in cSId:
            if cSId == ['10']:
                EId = cnx1.cursor(buffered=True)
                GetRandomEId = (
                    "select id from Etable order by rand() limit 1;")
                EID.execute(GetRandomEId)
                for row in EID:
                    RandomEId = (row[0])
                    print("10>>RandomEId>>", RandomEId)

            elif cSId == ['11']:
                VID = cnx1.cursor(buffered=True)
                GetRandomVId = (
                    "select id from Vtable order by rand() limit 1;")
                VID.execute(RandomVId)
                for row in VID:
                    RandomVId = (row[0])
                    print("11>>RandomVId>>", RandomVId)

            elif cSId == ['12']:
                PID = cnx1.cursor(buffered=True)
                GetRandomPId = (
                    "select id from Ptable order by rand() limit 1;")
                PID.execute(GetRandomPId)
                for row in PID:
                    RandomPId = (row[0])
                    print("12>>RandomPId>>", RandomPId)

            elif cSId == ['13']:
                RId = cnx1.cursor(buffered=True)
                GetRId = (
                    "select id from Rtable order by rand() limit 1;")
                RId.execute(GetRId)
                for row in RId:
                    RId = (row[0])
                    print("13>>RId>>", RId)

        test_list = (mt, cSId, num, aMB, uc)
        print(test_list)
    except KeyError:
        pass


#above code block give op like this:
##12>>PId>> b03433232321211s35ck
##('getmt1', ['12'], 1, F, 'iP/hPg/PFYo')
##11>>>> eqerefcdap07rd3lx
##('getmt2', ['11'], 2, F, 'isP/hPg/VFYo')
##13>>>> eqeeeqerefcdap07rd3lx
##('getmt3', ['13'], 2, F, 'isP/hPg/V_FY_o')
##('getmt4', ['13'], 12, F, 'isP/hPg/VFYo')
##('getmt5', ['11', '12'], 32, F, 'is-P/hPg/V-FY-o')

#create pytest requests

def test_allAPIsResponse():
            fURL = (url + "UC")
            headers = {'Accept': "application/json", 'Content-Type': "application/json", 'Accept-Encoding': "gzip, deflate",
                       'Cache-Control': "no-cache", 'Token': "null"}
            req_json = "{ \r\n\"reqP\": { \r\n \"cIid\":[\"[csID]<VID or PID or RID>\"] \r\n } \r\n}" #need to pass cSId here
            uid = {"userId": "Test1"}
            response = requests.request("POST", fURL, data=req_json, headers=headers, params=uid)
            jsondata = json.loads(response.content)
assert response.status_code == 200, "Test pass"

def test_allAPIsResponseCont():
            fURL = (url + uc)
            headers = {'Accept': "application/json", 'Content-Type': "application/json", 'Accept-Encoding': "gzip, deflate",
                       'Cache-Control': "no-cache", 'Token': "null"}
            req_json = "{ \r\n\"reqP\": { \r\n \"cIid\":[\"<VID or PID or RID>\"] \r\n } \r\n}" #no need to pass cSId here
            uid = {"userId": "Test1"}
            response = requests.request("POST", fURL, data=req_json, headers=headers, params=uid)
            jsondata = json.loads(response.content)
assert jsondata["rec"]["recO"]["recOO"][0]["csID"] == 1
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Ex of post req:
url = "http:/<domain>:<port>/coer/ucc/is-P%2fhPg%2fV-FY-o?userId=test1"
payload = "{\n\"reqparam\" : {\n\"aMB\":f,\n\"csID\":\"b03433232321211s35ck\"}\n}"
headers = {
'Accept': 'application/json',
'Content-Type': 'application/json',
'Content-Type': 'text/plain'
}

response = requests.request("POST", url, headers=headers, data = payload)