"""
功能:登錄接口的業務實現測試用例
"""import unittest
from api.api_login import ApiLogin
from parameterized import parameterized
from tools.read_json import ReadJson
#讀取json數據defget_data():
datas=ReadJson("login.json").read_json()#建立空列表,添加讀取的json數據
arrs=[]#遍歷多條json數據用例for data in datas.values():
arrs.append((data.get("url"),
data.get("mobile"),
data.get("code"),
data.get("expect_result"),
data.get("status_code")))return arrs
#登錄測試類classTestLogin(unittest.TestCase):#使用參數化動態獲取參數數據
@parameterized.expand(get_data())deftest_login(self,url,mobile,code,expect_result,status_code):#設置臨時靜態參數,url、mobile、code(有效一分鐘)# url="http://ttapi.research.itcast.cn/app/v1_0/authorizations"# mobile="18264152106"# code="965754"#調用登錄方法,返回響應對象
result=ApiLogin().api_post_login(url,mobile,code)print("響應結果:",result.json())#斷言,響應信息,狀態碼
self.assertEquals(expect_result,result.json()['message'])
self.assertEquals(status_code,result.status_code)if __name__ =='__main__':
unittest.main()# 響應結果: {'message': 'OK', 'data': {'token': 'eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJleHAiOjE1ODUxMzA1ODksInVzZXJfaWQiOjEyMzEwNTIyNDk1Njg5MDMxNjgsInJlZnJlc2giOmZhbHNlfQ.QFy0lXJ1So4bwJDLDwjbC6o2KeZyFYnYf2uMz-OAyMo', 'refresh_token': 'eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJleHAiOjE1ODYzMzI5ODksInVzZXJfaWQiOjEyMzEwNTIyNDk1Njg5MDMxNjgsInJlZnJlc2giOnRydWV9.TcgtEJs1p87aM5k5g9gqe46uSvddR_9MA9dMkpf9uVQ'}}
4、json數據login.json
{"login001":{"url":"http://ttapi.research.itcast.cn/app/v1_0/authorizations","mobile":"18264152106","code":"222337","expect_result":"OK","status_code":201},"login002":{"url":"http://ttapi.research.itcast.cn/app/v1_0/authorizations","mobile":"18264152101","code":"222337","expect_result":{"mobile":"18264152101 is not a valid mobile"},"status_code":400}}