要求:
1、調用登錄login
2、調用通過登錄接口返回的reponse中的token和uuid,實現test_create_todo接口的測試
實現:
1、login登錄接口的調用,直接填寫對應的URL、headers和data即可。再不需要其他參數的加入
2、因test_create_todo此接口在登錄后,因此需要用到token和uuid,由於是2個不同的方法,因此需要將使用的變量設置為全局變量。
實操作如下:
#!/usr/bin/env python # coding=UTF-8 import requests def login(): url = "https://***.***.com/v2/user/login" # 注:此處為本公司的host因此不方便外漏 data = { "mobile": "12606666333", "password": "33a7d3da476a32ac237b3f603a1be62fad00299e0d4b5a8db8d913104edec629" } headers = { "version": "2.3.0", "version-id": "235", "device-id": "8BAFD18C-61E3-4BAF-8AB1-0BC16E567633", "time": "1557728866628", "channel-id": "001", "os": "ios", "Accept-Language": "zh-tw", "device-name": "iPhoneX", "User-Agent": "iBer/235 CFNetwork/976 Darwin/18.2.0", #注:一定不能加content-type,否則報簽名錯誤 # Content-Type: multipart/form-data; boundary=vHsPJ5s6grMzpmWoZxd3T3o6.LcUWBvUUu0gDNubaf05Ve7kv6bAkH3s_rr0AEc2D6AbEh "sign": "a81b4379f504f330e83792ce2015e629" } r = requests.post(url=url, data=data, headers=headers) #global uuid,token,version,version_id global uuid uuid = str(r.json()["data"]["uuid"]) global token token = str(r.json()["data"]["token"]) version = "2.2.1" version_id = "232" print "登錄成功,如下是reponse返回的內容" print r.text #print uuid,token,version,version_id def test_create_todo(): url = "https://***.***.com/v2/todo/create" data = { "name": "1239", "todo_remind_type": "0", "cate_uuid":"86799e50d9890ade579c4ac88059a5ff", "all_day":"1", "todo_start":"2019-05-13", "todo_end":"", "type":"0", "repeat_tyep":"0", "c_user_uuid":"" } headers = { "version": "2.3.0", "version-id": "235", "os": "ios", "sign": "123456", "is-test":"1", "uuid":uuid, "token":token } print uuid print token r = requests.post(url=url, data=data, headers=headers) print "%%%%%%%%%%%%%%%%%%" print r.json() login() test_create_todo()
注:使用postman工具的執行步驟,可參考此鏈接:https://www.cnblogs.com/syw20170419/p/10858645.html