基于上一篇参数化的梳理,本篇用另一种方法从数据库中取出这100个用户来登录
思路:在 TaskSet 中的 on_start 方法表示执行任务前的操作,可以将数据库取数据的这个操作放在里面执行
from locust import task,TaskSet,HttpLocust import pymysql import os class MyTaskSet(TaskSet): def run_sql(self): self.connect = pymysql.connect( host = "192.168.x.xx", database = "xxx", user = "xxx", password = "xx" ) cursor = self.connect.cursor() sql = "SELECT userAccount FROM t_sys_user WHERE userAccount LIKE ‘w0%‘;" # 查询出用户名语句 cursor.execute(sql) # 执行查询语句 res = cursor.fetchall() # 保存查询结果 self.user_list = [] # 查询出来的结果为元组中嵌套长度为1的元组,不方便取值,这里用列表保存下来 for i in res: self.user_list.append(i[0]) print(self.user_list) cursor.close() self.connect.commit() self.connect.close() return self.user_list # 返回列表 def on_start(self): self.run_sql() # 执行task前运行一次run_sql函数 def on_stop(self): print("----- Test over -----") @task def login(self,pwd="b123456"): self.headers = { "Origin": "http://192.168.1.9:8080", "User-Agent": "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.110 Safari/537.36" } path = "/SSOAuth?action=login&code=undefined&gotoURL=http%3A%2F%2F192.168.1.9%3A8080%2Fportal%2Fgeoindex.do" for user_account in self.user_list: da = {"userAccount": user_account, "pwd": pwd} data = { "data": ‘%s‘ % da } self.client.post(path, headers=self.headers, data=data, verify=False) class MyLocust(HttpLocust): """w01~w0100为有效用户名,密码为默认的b123456""" task_set = MyTaskSet # 指向任务集合 host = "http://192.168.1.9:8080" # web项目的话这里要设置host属性,否则是报错的 if __name__ == ‘__main__‘: # os模块执行系统命令,相当于在cmd切换到当前脚本目录,执行locust -f locust_login.py os.system("locust -f locust_login_db.py")
原文地址https://blog.csdn.net/qq_39620483/article/details/89639653
很好的博客https://blog.csdn.net/qq_39620483
原文地址:https://www.cnblogs.com/111testing/p/10998726.html
时间: 2024-10-31 02:29:30