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)
mock测试
最新推荐文章于 2024-07-07 21:51:09 发布