导:使用python代码实现mock接口返回数据,框架用的是unittest
目录:
这个是自定义的接口类方法(http_requests):
import requests
import json
class RunMain:
# def __init__(self,url,method,data=None):
# self.result = self.runmain(url,method,data) #init方法调用时候会自动执行
# print(self.result)
#接口是get
def send_get(self, url, data=None):
result = requests.get(url=url, params=data)
return result.json()
#接口是post
def send_post(self, url, data=None):
result = requests.post(url=url, data=data)
return result.json()
def runmain(self,url,method,data):
res = None #先空值,调用后,再打印出来
if method == "GET":
res = self.send_get(url,data)
elif method == "POST":
res = self.send_post(url,data)
# return json.dumps(res,indent=2,so