python脚本之mock接口

Posted 今夜月色很美

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python脚本之mock接口相关的知识,希望对你有一定的参考价值。

生成get post常见参数类型测试mock接口

# coding=UTF-8
from flask import Flask, jsonify, request
from flask import Response
import json,datetime
import demjson
app = Flask(__name__)


@app.route('/')
def hello_world():
    return 'Hello World!'


@app.route('/memberlist')
def member_list():

    ret = {'retcode': '000000', 'members': [{'name': 'zhangsan', 'age': 18}, {'name': 'lisi', 'age': 20}]}
    # time.sleep(1)
    return jsonify(ret)

##Content-type: application/x-www-form-urlencoded
@app.route('/memberinfo', methods=['GET','POST'])
def member_get():
    print("method=========", request.method)
    if request.method == 'GET':
        print(1)
        print(request.headers)
        print(request.args)
        print(2)
        if (request.args.get("name") == 'zhangsan1'):
            ret = {'retcode': '000000', 'name': 'zhangsan', 'age': 18}
        elif (request.args.get("name") == 'lisi'):
            ret = {'retcode': '000000', '姓名': '李四', '年龄': 20}
        elif (request.args.get("name") == 'wangwu'):
            ret = {'retcode': '000000', '姓名': '王五', '年龄': 22}
        else:
            ret = {'retcode': '100000', 'msg': 'can not find member : ' + request.args.get("name")}
        return jsonify(ret)
    else:
        try:
            print(1)
            print(request.headers)
            print(request.data)
            print(request.form)
            print(2)
            username = request.form.get('name')
            print(username)
            if (username == 'zhangsan'):
                ret = {'retcode': '000000', 'name': 'zhangsan', 'age': 18}
            elif (username == 'lisi'):
                ret = {'retcode': '000000', 'name': 'lisi', 'age': 20}
            elif (username == 'wangwu'):
                ret = {'retcode': '000000', '姓名': '王五', '年龄': 22}
            else:
                ret = {'retcode': '100000', 'msg': 'can not find member : ' + request.args.get("name")}
            return jsonify(ret)
        except Exception as e:
            import traceback
            traceback.print_exc()
            return 'error'


##Content-type: application/json
@app.route('/memberinfo_json', methods=['POST'])
def memberinfo_json():
    try:
        print(1)
        print(request.headers)
        print(request.data)
        print(2)
        username = demjson.decode(request.data.decode(encoding='utf-8')).get('name')
        if (username == 'zhangsan'):
            ret = {'retcode': '000000', 'name': 'zhangsan', 'age': 18}
        elif (username == 'lisi'):
            ret = {'retcode': '000000', 'name': 'lisi', 'age': 20}
        elif (username == 'wangwu'):
            ret = {'retcode': '000000', '姓名': '王五', '年龄': 22}
        else:
            ret = {'retcode': '100000', 'msg': 'can not find member : ' + request.args.get("name")}
        return jsonify(ret)
    except Exception as e:
        return 'error'



@app.route('/memberinfo_param', methods=['POST'])
def memberinfo_param():
    try:
        print(1)
        print(request.headers)
        print(request.args)
        print(2)
        username = request.args.get("name")
        if (username == 'zhangsan'):
            ret = {'retcode': '000000', 'name': 'zhangsan', 'age': 18}
        elif (username == 'lisi'):
            ret = {'retcode': '000000', 'name': 'lisi', 'age': 20}
        elif (username == 'wangwu'):
            ret = {'retcode': '000000', '姓名': '王五', '年龄': 22}
        else:
            ret = {'retcode': '100000', 'msg': 'can not find member : ' + request.args.get("name")}
        return jsonify(ret)
    except Exception as e:
        return 'error'



##Content-type: multipart/form-data
@app.route('/upload', methods=['POST', 'GET'])
def upload():
    if request.method == 'POST':
        print(1)
        f = request.files['file']
        return f.filename


if __name__ == '__main__':
    app.run(host="0.0.0.0")

以上是关于python脚本之mock接口的主要内容,如果未能解决你的问题,请参考以下文章

测试开发系列之Python开发mock接口

Python之mock接口开发

Python之mock接口开发

测试开发系列之Python开发mock接口

python 接口测试之Mock

测试开发系列之Python开发mock接口