用户频道列表接口测试
1、自动化测试的目录结构
见文章
2、实现用户频道列表接口的对象封装api_channels.py
"""
功能:实现用户频道列表接口的对象封装
"""
import requests
class ApiChannels(object):
def api_get_channels(self,url,headers):
return requests.get(url,headers=headers)
3、获取用户频道列表的业务实现test_channels.py
"""
功能:获取用户频道列表的业务实现
"""
import unittest
from api.api_channels import ApiChannels
from parameterized import parameterized
from tools.read_json import ReadJson
def get_data():
datas=ReadJson("channels.json").read_json()
arrs=[]
for data in datas.values():
arrs.append((data.get("url"),
data.get("headers"),
data.get("expect_result"),
data.get("status_code")))
return arrs
class TestChannels(unittest.TestCase):
@parameterized.expand(get_data())
def test_channels(self,url,headers,expect_result,status_code):
result=ApiChannels().api_get_channels(url,headers)
print("响应结果:",result.json())
self.assertEquals(status_code,result.status_code)
self.assertEquals(expect_result,result.json()['message'])
if __name__ == '__main__':
unittest.main()
4、json数据channels.json
{
"channels001":{
"url":"http://ttapi.research.itcast.cn/app/v1_0/user/channels",
"headers": {"Content-Type":"application/json", "Authorization":"Bearer eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJleHAiOjE1ODUxMzA1ODksInVzZXJfaWQiOjEyMzEwNTIyNDk1Njg5MDMxNjgsInJlZnJlc2giOmZhbHNlfQ.QFy0lXJ1So4bwJDLDwjbC6o2KeZyFYnYf2uMz-OAyMo"},
"expect_result":"OK",
"status_code":200
},
"channels002":{
"url":"http://ttapi.research.itcast.cn/app/v1_0/user/channels",
"headers": {"Content-Type":"application/json", "Authorization":"Bearer eyJ0eXssAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJleHAiOjE1ODUxMzA1ODksInVzZXJfaWQiOjEyMzEwNTIyNDk1Njg5MDMxNjgsInJlZnJlc2giOmZhbHNlfQ.QFy0lXJ1So4bwJDLDwjbC6o2KeZyFYnYf2uMz-OAyMo"},
"expect_result":"OK",
"status_code":200
}
}
5、读取json数据工具类reas_json.py
"""
功能:读取json数据,返回json对象
"""
import json
class ReadJson(object):
def __init__(self,fileName):
self.filePath="../data/"+fileName
def read_json(self):
with open(self.filePath,"r",encoding="utf-8") as f:
return json.load(f)