接口测试 -- 保存 requests 的 cookies
session是保存在服务器中的,而cookies是保存在客户端中的。
服务器通过session id来跟踪客户,而对于客户端而言,session id是保存在cookies中的,所以只要把cookies保存下来就行
json.dumps(session.cookies.get_dict())) # 保存
session.cookies.update(json.loads(f.read())) # 读取
具体实现:
token_post.py
1 import requests 2 import json 3 4 file_path = "./conf/" 5 6 class usertoken_post(object): 7 8 def token_post(self): 9 self.url = "http://192.168.68.65:8086/" 10 self.s = requests.session() 11 self.action_type = "login/userLogin" 12 self.params = { 13 "userAcc": "xxxxx", 14 "userPwd": "xxxx" 15 } 16 17 self.r = self.s.post(self.url + self.action_type, params=self.params) 18 19 with open(file_path + 'token.txt', 'w') as f_token: 20 f_token.write(self.r.json()['msg']) 21 f_token.close() 22 23 with open(file_path + 'url.txt', 'w') as f_url: 24 f_url.write(self.url) 25 f_url.close() 26 27 with open(file_path + 'cookies.txt', 'w') as f_s: 28 f_s.write(json.dumps(self.s.cookies.get_dict())) # 保存 session 29 f_s.close() 30 31 self.s.close()
read_global:
from token_post import usertoken_post import requests, json file_path = "./conf/" class read_gvar(object): def get_gvar(self): with open(file_path + 'token.txt', 'r') as f_token: token = f_token.read() f_token.close() with open(file_path + 'url.txt', 'r') as f_url: url = f_url.read() f_url.close() with open(file_path + 'cookies.txt', 'r') as f_cookies: cookies = f_cookies.read() f_cookies.close() # 判断当前 session 的 cookies 是否已过期,如果过期,则重新生成 cookies, token self.action_type = "sensitiveWord/search" self.params = { "functions": "sense_M", "userToken": token, "pattern": "a" } self.sl = requests.session() self.sl.cookies.update(json.loads(cookies)) self.r = self.sl.post(url + self.action_type, params=self.params) if "登录超时" in self.r.text: usertoken_post().token_post() with open(file_path + 'cookies.txt', 'r') as f_cookies: cookies = f_cookies.read() f_cookies.close() with open(file_path + 'token.txt', 'r') as f_token: token = f_token.read() f_token.close() self.sl.close() return url, token, cookies
test_sensitive_word:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
import requests import json import unittest import time from read_globalvar import read_gvar # 定义全局变量 url, token, cookies = read_gvar().get_gvar() now = "test" + time.strftime("%Y%m%d%H%M%S") path_testfile = "./test_case/" #path_testfile = "./" #重新开启一个 session 并 更新 cookies,继续连接 sl = requests.session() sl.cookies.update(json.loads(cookies)) class MyTest(unittest.TestCase): def setUp(self): print("start test") pass def tearDown(self): print("end test") pass class sensitiveword(MyTest): wid = -100 # 定义静态变量 def close_req_files(self): for f in self.files.values(): f.close() def test1_1_sensitiveword_add(self): # 新增一个敏感词 self.action_type = "sensitiveWord/add" self.params = { "functions": "sense_M", "userToken": token, "w": now } self.r = sl.post(url + self.action_type, params=self.params) print(self.r.text) self.assertEqual(200, self.r.status_code) self.assertIn("请求成功", self.r.text) sl.close() if __name__ == "__main__": unittest.main()