如何解决在蝗虫类中使用setattr
我正在使用Locust对API进行负载测试。举例来说,我正在向Google发送GET请求。我的用例是我想通过setattr通过将函数动态分配给类来创建@task方法。这是我的代码
from locust import TaskSet,HttpUser,between,task
@task
def default_method(self):
self.client.get("/")
class WebsiteTasks(TaskSet):
def on_start(self):
print("start process")
setattr(self,'index',default_method)
class WebsiteUser(HttpUser):
host = "https://www.google.com"
tasks = [WebsiteTasks]
wait_time = between(1,10)
作为响应,我在启动新的负载测试浏览器时遇到该异常:
Exception: No tasks defined. use the @task decorator or set the tasks property of the User
任何帮助将不胜感激
解决方法
在on_start中添加任务为时已晚,因为Locust会在实际启动之前检查任务是否存在(我认为这是正确的行为,否则人们可能最终运行on_start只是在测试之后立即失败)。 / p>
是什么使您无法使用列表,例如文档中的示例:
def my_task(user):
pass
class MyUser(User):
tasks = [my_task]
...
只要列表开头不为空,您仍然可以从on_start操作该列表。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。