python -第七节课之接口开发mock接口

开发接口

实际场景,银行接口连接不通我们无法测试,需要我们开发接口去调银行的接口

业务流程连贯起来

 测试场景写一个假的调用我们的不阻塞测试,原来返回的数据和测试场景我们自己构造

不会的话还要让别人给你写接口,这是开发接口的需要

python 依赖模块 mock 模块 

 安装模块 

fastapi
uvicorn

写一个传参数的get接口
import fastapi
import uvicorn
server=fastapi.FastAPI()#服务
@server.get('/login')#装饰器@在浏览器可以访问路径/login
def login(username:str,password:str):
return {'username':username,'password':password}
uvicorn.run(server,port=8888,debug=True)
写一个自动返回的get接口
import fastapi
import uvicorn
server=fastapi.FastAPI()#服务
@server.get('/login')#装饰器@在浏览器可以访问路径/login
def login(username:str,password:str):
return {'username':username,'password':password}

@server.get('/test')#装饰器@在浏览器可以访问路径/login
def login():
return {'msg':'你好'}
uvicorn.run(server,port=8888,debug=True)

在浏览器输入http://127.0.0.1:8888/docs 可以查看接口的信息

http://127.0.0.1:8888/docs  代表本地ip自己访问让别人访问就要修改 host='0.0.0.0'
uvicorn.run(server,port=8888,debug=True,host='0.0.0.0')
也可以使用flask模块一般都是有flask接口

如果你想根据传的字段类型判断
@server.get('/pay')
def pay(money:float,status='1'):
if status=='0':
return {'code':1,'status':'fail'}
elif status=='1':
return {'code':0,'status':'success','balance':money}
http://127.0.0.1:8888/pay?money='200'&status='1'
如果你想调用数据库写一个注册接口怎么写呢?

@server.post('/reg')
def reg(username:str,password:str,cpassword:str):
if username.strip() and password.strip() and cpassword.strip():
if password.strip() != cpassword.strip():
return {'code': -1, 'msg': '两次输入的密码不一样'}
else:
sql='select * from app_myuser where username="%s";'%username
if tools.execute_sql(sql):
return {'code':-1,'msg':'用户已经存在'}
else:
p = tools.my_md5(password)
insert_sql = 'insert into app_myuser (username,passwd) value ("%s","%s");'%(username,p)
tools.execute_sql(insert_sql)
return {'code':0,'msg':'注册成功!'}
 
else:
return {'code':-1,'msg':'必填参数不能为空'}

 写个登录的接口

 

@server.get('/login')
def login(username: str, password: str):
if username.strip() and password.strip():
p = tools.my_md5(password)
query_sql = 'select * from app_myuser where username= "%s" and passwd=%s;' % (username, p)
if tools.excute(query_sql):
return {'code': '0', 'msg': '登录成功'}
else:
return {'code': '-1', 'msg': '输入的用户名/密码错误'}
else:
return {'code': '-1', 'msg': '不能为空'}
 
fastapi的优点可以生成接口文档
 下面介绍flask模块的应用
import flask
#轻量级的web开发框架
import tools
import json
server = flask.Flask(__name__)
 
 
@server.route('/login',methods=['post','get'])
def login():
username = flask.request.values.get('username','')
password = flask.request.values.get('password','')
# print(flask.request.cookies.get('PHPSESSID'))
# print('json',flask.request.json)
# flask.json.get('xxxx')#如果入参是json类型的话,这么搞
# flask.request.cookies.get('xxx')#获取cookie里面的数据
# flask.request.headers.get('xx')
# flask.request.files.get("xxx")#文件
 
if username.strip() and password.strip():
p = tools.my_md5(password)
query_sql = 'select * from app_myuser where username= "%s" and passwd="%s";' % (username, p)
if tools.execute_sql(query_sql):
return json.dumps({'code': '0', 'msg': '登录成功','sessionid':'xxxx'},ensure_ascii=False)
else:
return json.dumps({'code': '-1', 'msg': '输入的用户名/密码错误'})
else:
return json.dumps({'code': '-1', 'msg': '不能为空'})
 
@server.route('/reg',methods=['post','get'])
def reg():
username = flask.request.values.get('username')
password = flask.request.values.get('password')
cpassword = flask.request.values.get('cpassword')
if username.strip() and password.strip() and cpassword.strip():
if password.strip() != cpassword.strip():
return json.dumps({'code': -1, 'msg': '两次输入的密码不一样'})
else:
sql='select * from app_myuser where username="%s";'%username
if tools.execute_sql(sql):
return json.dumps({'code':-1,'msg':'用户已经存在'})
else:
p = tools.my_md5(password)
insert_sql = 'insert into app_myuser (username,passwd) value ("%s","%s");'%(username,p)
tools.execute_sql(insert_sql)
return json.dumps({'code':0,'msg':'注册成功!'},ensure_ascii=False)
 
else:
return json.dumps({'code':-1,'msg':'必填参数不能为空'})
 
 
 
server.run(host='0.0.0.0',port=8999,debug=True)

 我们要学会一种写法要熟练使用,比较好用的模块就是flask模块  

flask 是一个轻量级的web平台框架  

 

import flask,json
server = flask.Flask(__name__)
import tools
@server.route('/login', methods=['post', 'get'])#写接口路径
def login():
#写参数
username = flask.request.values.get('username', '')#传values,get和post都可以获取到
password = flask.request.values.get('password', '')
# print(flask.request.cookies.get('PHPSESSID'))#获取cookie
# print('json',flask.request.json)
# flask.json.get('xxxx')#如果入参是json类型的话,这么搞
# flask.request.cookies.get('xxx')#获取cookie里面的数据
# flask.request.headers.get('xx')
# flask.request.files.get("xxx")#文件
if username.strip() and password.strip():
p=tools.my_md5(password)#把密码转成md5
query_sql = 'select * from app_myuser where username= "%s" and passwd="%s";' % (username, p)#和数据库比对
print(query_sql)
if tools.execute_sql(query_sql):
return json.dumps ({'code':'0','msg':'登录成功'},ensure_ascii=False)
else:
return json.dumps({'code':'1','msg':'输入的用户名和密码错误'},ensure_ascii=False)
else:
return json.dumps({'code': '-1', 'msg': '不能为空'})
@server.route('/reg', methods=['post', 'get'])#写接口路径
def reg():
username = flask.request.values.get('username')
password = flask.request.values.get('password')
cpassword = flask.request.values.get('cpassword')
if username.strip() and password.strip() and cpassword.strip():
if password.strip() != cpassword.strip():
return json.dumps({'code': -1, 'msg': '两次输入的密码不一样'})
else:
sql = 'select * from app_myuser where username="%s";' % username
if tools.execute_sql(sql):
return json.dumps({'code': -1, 'msg': '用户已经存在'})
else:
p = tools.my_md5(password)
insert_sql = 'insert into app_myuser (username,passwd) value ("%s","%s");' % (username, p)
tools.execute_sql(insert_sql)
return json.dumps({'code': 0, 'msg': '注册成功!'}, ensure_ascii=False)#加ensurte显示中文

else:
return json.dumps({'code': -1, 'msg': '必填参数不能为空'})

server.run(host='0.0.0.0',port=8999,debug=True)
host写 0.0.0.就可以让别人访问了

 刚开始写是自己一个一个写,熟练的话自己开发一个mock接口开发平台 这是你要最终实现的操作  

开发好了交给别人操作,让别人录制接口,以后想mock 让别人操作的

 这一步你就可以开发测试接口了



posted @ 2020-09-18 16:06  测试董先生  阅读(244)  评论(0编辑  收藏  举报