使用python test测试http接口
-
获取token和控制session,后面大多数接口要带上这些信息
import time import requestsfrom common.aes_algorithm import AES from config.config import Config from config.log import logclass Common:username = "admin"password = "888888"token = Noneuser_id = NonecontrolSession = Nonedef __init__(self):pass@classmethoddef login(cls):timestamp = int(time.time())password = AES.encrypt(cls.password, str(timestamp))data = {"username": cls.username,"password": password,"timestamp": timestamp,}response = requests.post(f"{Config.base_url}/user/login", json=data)response.raise_for_status()# 打印响应内容dataJson = response.json()log.debug(dataJson)if dataJson["status"] == 200:data = dataJson.get("data")cls.token = data.get("Authorization")cls.user_id = data.get("userID")@classmethoddef get_control_session(cls):if cls.controlSession is None:timestamp = int(time.time())password = AES.encrypt(cls.password, str(timestamp))response = requests.post(f"{Config.base_url}/user/verifyControlPasswd",headers={"Authorization": cls.token},json={"userID": cls.user_id,"password": password,"timestamp": timestamp,},)response.raise_for_status()dataJson = response.json()log.debug(dataJson)if dataJson["status"] == 200:data = dataJson.get("data")cls.controlSession = data.get("controlSession")def get_token(cls):cls.login()cls.get_control_session()if __name__ == "__main__":common = Common()common.get_token()
-
使用unittest编写单元测试
import time import unittestimport requestsfrom common.login import Common from config.config import Config from config.log import logclass StgyModeTest(unittest.TestCase):@classmethoddef setUpClass(cls):cls.common = Common()cls.common.get_token() # 只执行一次timestamp = int(time.time())cls.data = [{"userID": Common.user_id,"controlSession": Common.controlSession,"timestamp": timestamp,"mode": "powerBalanced",},{"userID": Common.user_id,"controlSession": Common.controlSession,"timestamp": timestamp,"mode": "socBalanced",},{"userID": Common.user_id,"controlSession": Common.controlSession,"timestamp": timestamp,"mode": "customRatio","powerWeights": [{"pcsCode": "pcs1", "ratio": 0.3},{"pcsCode": "pcs2", "ratio": 0.7},{"pcsCode": "pcs3", "ratio": 0},],},]def test_set_power_balanced_stgy(self):response = requests.post(f"{Config.base_url}/strategy/setStrategyMode",headers={"Authorization": Common.token},json=self.data[0],)log.info(f"set stgy mode response: {response.text}")response.raise_for_status()# 打印响应内容dataJson = response.json()if dataJson["status"] == 200:data = dataJson.get("data")log.info(f"set stgy mode success, data: {data}")def test_set_soc_balanced_stgy(self):response = requests.post(f"{Config.base_url}/strategy/setStrategyMode",headers={"Authorization": Common.token},json=self.data[1],)log.info(f"set stgy mode response: {response.text}")response.raise_for_status()# 打印响应内容dataJson = response.json()if dataJson["status"] == 200:data = dataJson.get("data")log.info(f"set stgy mode success, data: {data}")def test_set_custom_power_weights_stgy(self):response = requests.post(f"{Config.base_url}/strategy/setStrategyMode",headers={"Authorization": Common.token},json=self.data[2],)log.info(f"set stgy mode response: {response.text}")response.raise_for_status()# 打印响应内容dataJson = response.json()if dataJson["status"] == 200:data = dataJson.get("data")log.info(f"set stgy mode success, data: {data}")# 获取策略接口def test_get_stgy_mode(self):response = requests.get(f"{Config.base_url}/strategy/getStrategyMode",headers={"Authorization": Common.token},)log.info(f"get stgy mode response: {response.text}")if __name__ == "__main__":unittest.main()
vscode里面启用unittest插件
-
代开vscode设置,搜索python test,启用unittest
-
在左侧可以运行单个测试