jmeter性能测试练习及断言
1、实战应用:
a.新建项目:
b.选中ApiTest项目(鼠标右键)——>添加——>监听器——>察看结果树,
c.再选中ApiTest项目(鼠标右键)——>添加——>线程(用户)——>线程组
d.下一步,再选中线程组(鼠标右键)——>添加——>逻辑控制器——>简单控制器
e.下一步,再选中简单控制器(鼠标右键)——>添加——>配置元件——>HTTP信息头管理器(主要填写headers里面的内容如下图第二张,以拉勾网为例)
f.下一步,再选中简单控制器(鼠标右键)——>添加——>取样器——>HTTP请求
g.最后运行出来的结果,
2、练习:
(1)实例一:login.py文件
#!/usr/bin/env python #!coding:utf-8 from flask import Flask,jsonify from flask_restful import Api,Resource,reqparse app=Flask(__name__) api=Api(app) class LoginView(Resource): def get(self): return {'status':0,'msg':'ok','data':'this is a login page'} def post(self): parser=reqparse.RequestParser() parser.add_argument('username', type=str, required=True, help='用户名不能为空') parser.add_argument('password',type=str,required=True,help='账户密码不能为空') parser.add_argument('age',type=int,help='年龄必须为正正数') parser.add_argument('sex',type=str,help='性别只能是男或者女',choices=['女','男']) args=parser.parse_args() return jsonify(args) api.add_resource(LoginView,'/login',endpoint='login') if __name__ == '__main__': app.run(debug=True,port=5000)
(2)在pycharm中运行后,再在jmeter中开始以下步骤:
(3)测试从以下几个方面去执行:
a、login登陆成功:
b、login登录性别不是男或者女
c、login登录年龄不是正整数
d、剩下的例子步骤如上,最后的结果如下图:
(4)、实例二:book.py文件
from flask import Flask,make_response,jsonify,abort,request from flask_restful import Api,Resource from flask_httpauth import HTTPBasicAuth # from skywalking import agent,config # config.init(collector='http://localhost:8080/', service='saas') #采集服务的地址,给自己的服务起个名称 # config.flask_collect_http_params = True # flask接收到的http参数也保存 # agent.start() from flask import Flask from flask_jwt import JWT, jwt_required, current_identity from werkzeug.security import safe_str_cmp app=Flask(__name__) app.debug = True app.config['SECRET_KEY'] = 'super-secret' api=Api(app=app) auth=HTTPBasicAuth() @auth.get_password def get_password(name): if name=='admin': return 'admin' @auth.error_handler def authoorized(): return make_response(jsonify({'msg':"请认证"}),403) books=[ {'id':1,'author':'wuya','name':'Python接口自动化测试实战','done':True}, {'id':2,'author':'无涯','name':'Selenium3自动化测试实战','done':False} ] class User(object): def __init__(self, id, username, password): self.id = id self.username = username self.password = password def __str__(self): return "User(id='%s')" % self.id users = [ User(1, 'wuya', 'asd888'), User(2, 'admin', 'asd888'), User(3,'share','asd888') ] username_table = {u.username: u for u in users} userid_table = {u.id: u for u in users} def authenticate(username, password): user = username_table.get(username, None) if user and safe_str_cmp(user.password.encode('utf-8'), password.encode('utf-8')): return user def identity(payload): user_id = payload['identity'] return userid_table.get(user_id, None) jwt = JWT(app, authenticate, identity) class Books(Resource): # decorators = [auth.login_required] decorators=[jwt_required()] def get(self): return jsonify({'status':0,'msg':'ok','datas':books}) def post(self): if not request.json: return jsonify({'status':1001,'msg':'请求参数不是JSON的数据,请检查,谢谢!'}) else: book = { 'id': books[-1]['id'] + 1, 'author': request.json.get('author'), 'name': request.json.get('name'), 'done': True } books.append(book) return {'status':1002,'msg': '添加书籍成功','datas':book} # return jsonify({'status':1002,'msg': '添加书籍成功','datas':book}, 201) class Book(Resource): # decorators = [auth.login_required] decorators = [jwt_required()] def get(self,book_id): book = list(filter(lambda t: t['id'] == book_id, books)) if len(book) == 0: return jsonify({'status': 1003, 'msg': '很抱歉,您查询的书的信息不存在'}) else: return jsonify({'status': 0, 'msg': 'ok', 'datas': book}) def put(self,book_id): book = list(filter(lambda t: t['id'] == book_id, books)) if len(book) == 0: return jsonify({'status': 1003, 'msg': '很抱歉,您查询的书的信息不存在'}) elif not request.json: return jsonify({'status': 1001, 'msg': '请求参数不是JSON的数据,请检查,谢谢!'}) elif 'author' not in request.json: return jsonify({'status': 1004, 'msg': '请求参数author不能为空'}) elif 'name' not in request.json: return jsonify({'status': 1005, 'msg': '请求参数name不能为空'}) elif 'done' not in request.json: return jsonify({'status': 1006, 'msg': '请求参数done不能为空'}) elif type(request.json['done'])!=bool: return jsonify({'status': 1007, 'msg': '请求参数done为bool类型'}) else: book[0]['author'] = request.json.get('author', book[0]['author']) book[0]['name'] = request.json.get('name', book[0]['name']) book[0]['done'] = request.json.get('done', book[0]['done']) return jsonify({'status': 1008, 'msg': '更新书的信息成功', 'datas': book}) def delete(self,book_id): book = list(filter(lambda t: t['id'] == book_id, books)) if len(book) == 0: return jsonify({'status': 1003, 'msg': '很抱歉,您查询的书的信息不存在'}) else: books.remove(book[0]) return jsonify({'status': 1009, 'msg': '删除书籍成功'}) api.add_resource(Books,'/v1/api/books') api.add_resource(Book,'/v1/api/book/<int:book_id>') if __name__ == '__main__': app.run(debug=True,host='0.0.0.0')
(5)在pycharm中运行后,再在jmeter中开始以下步骤:
a、创建一个用户自定义变量,如下图:
b、将端口的配置文件分离出来:
c、授权登录账户和密码:
d、授权登录后返回的access_token。
e、针对书籍的管理:
f、获取所有的书籍信息
g、运行的结果:
h、添加书籍
i、JSON提取器
三种断言方式(通过json断言):
最后的结果
j