Jmeter之文件上传

2020-07-13  本文已影响0人  是立品啊

在日常被测系统中,我们会经常遇到文件上传的操作,那么针对这样的接口,使用接口测试测试工具Jmeter需要怎么操作呢,今天就来带大家一起实际操作一下

前情描述:

因为是示例,所以在本地用Flask定义好上传接口,服务也是起在本地,以下是接口实现代码

UPLOAD_FOLDER = 'upload'
app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER
basedir = os.path.abspath(os.path.dirname(__file__))
ALLOWED_EXTENSIONS = set(['txt', 'png', 'jpeg','jpg', 'xls', 'JPG', 'PNG', 'xlsx', 'gif', 'GIF', 'doc', 'docx', 'ppt', 'pptx'])
# upload file
@app.route('/uploadfile', methods=['POST'], strict_slashes=False)
def api_upload():
    file_dir = os.path.join(basedir, app.config['UPLOAD_FOLDER'])
    if not os.path.exists(file_dir):
        os.makedirs(file_dir)
    f = request.files['file']  # get the file name

    if f and allowed_file(f.filename):  # Verify the file type
        fname = secure_filename(f.filename)
        ext = fname.rsplit('.', 1)[1]
        unix_time = time.time()
        new_filename = str(unix_time) + '.' + ext
        f.save(os.path.join(file_dir, new_filename))
        print(f'{f.filename} upload success!')
        return jsonify({'code': 200, 'result': 'success', 'msg': f'file {f.filename} upload success'})
    else:
        print(f'{f.filename} upload failed!')
        return jsonify(
            {'code': 500, 'result': 'failed', 'msg': f'file {f.filename} upload fail, please check the file type'})

上传文件步骤

Step1.抓取文件上传的接口相关参数

1. 启动示例程序,选择要上传的文件,点击上传

image

2.通过抓包工具抓取接口相关信息

image

这里几个参数要关注下

Step2:把抓到的接口用Jmter的请求的方式上传

  1. 打开Jmeter压测工具,创建对应的线程组,取样器

  2. 按照我们刚刚抓取的接口参数,完善取样器参数

image

Step3.点击发送,就可以看到结果树显示的正常结果

image

这样,一个正常使用Jmeter上传文件到接口就算完成了

上一篇 下一篇

猜你喜欢

热点阅读