mock测试

from flask import Flask,request,redirect,url_for,render_template
app=Flask(__name__)

#GET获取值
@app.route('/app/case01',methods=['GET'])
def case01():
    flag = request.args.get('flag')
    print(flag)
    return 'GET请求成功'

#POST获取值----要使用jemter查看mock是否正常返回数据
@app.route('/app/case02',methods=['POST'])
def case02():
    data=request.form.get('flags')
    print(data)
    return {'code':200,'msg':'succes'}
#<>表示装换---默认类型为str字符串
#加上path的好处---表示可以使用斜杠

# ------>http://127.0.0.1:8080/app/case03/ncicl
@app.route('/app/case03/<path:username>',methods=['GET'])
def case03(username):
    print(username)
    return {'code':200,'msg':'succes'}

#mock断言案例
@app.route('/app/case04',methods=['POST'])
def case04():
    with app.test_request_context('app/case04',method='POST'):
        assert request.path =='/app/case04', '路由错误'
        assert request.method == 'POST', 'method路由错误'
    print('执行了case04')
    return {'code':200,'msg':'succes'}

#重定向
@app.route('/')
def case05():
    return redirect(url_for('case06'))
@app.route('/hello')
def case06():
    print(11111)
    return {'code':200,'msg':'succes'}

#页面跳转
@app.route('/hello')
def case07():
    print(11111)
    return render_template('hell.html')
#页面跳转传变量
@app.route('/name/<username>')
def case07(username):
    print(11111)
    return render_template('hell.html',name=username)

@app.errorhandler(404)
def not_find(error):
    return render_template('hell.hrm'),404



if __name__ == '__main__':
    app.run(port=8080)
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值