我如何将变量传递给“@events.test_start.add_listener”函数

How would I pass variables to a `@events.test_start.add_listener` function

我正在尝试 运行 使用 Locust 进行负载测试,但我需要 运行 一些 API 调用,然后才能开始产卵工人。我已经将我的用户凭据作为环境变量传递,但我不想在 locustfile 中对主机进行硬编码。我希望拥有看起来像

的东西
@events.test_start.add_listener
def on_test_start(**kwargs):
    init(host, username, password)

其中 init 将处理所有初始 API 调用。

init 函数的第一个参数 environmentparsed_options 字典中包含蝗虫设置。尝试从那里获取主机。

类似(未测试)

@events.init.add_listener
def on_locust_init(environment, **kwargs):
    init(environment.parsed_options[”host”], ...)

我无法对 Cyber​​wiz 的回答发表评论,所以我 post 一个新的回答,希望你不介意。

我正在尝试做一些类似于你想做的事情,但我无法通过 environment.parsed_options["host"] 获得主机 url ,我收到一条错误消息,上面写着 'Namespace' object is not subscriptable

相反,我尝试使用 environment.host 并成功获得主机 url,希望这对遇到与我相同问题的人有所帮助!

参考:https://docs.locust.io/en/stable/api.html#environment-class