是否可以添加自定义蝗虫命令行参数

Is it possible to add custom locust command line arguments

我想知道是否有办法将自定义 command-line/configuration 参数传递给蝗虫。我目前正在将我的用户凭证作为环境变量传递到我的测试中,但我想通过命令行将它们传递进来,看起来像这样:

locust -f <locustfile> --host <host> --username <username> --password  <password>

理想情况下,我想避免创建我自己的自定义 locust 扩展,但我猜我必须为此创建。我很乐意听到任何建议。

是的! github 上有一个例子:https://github.com/locustio/locust/blob/master/examples/add_command_line_argument.py

@events.init_command_line_parser.add_listener
def init_parser(parser):
    parser.add_argument(
        '--my-argument',
        help="It's working"
    )

class WebsiteUser(HttpUser):
    @task
    def my_task(self):
        print(self.environment.parsed_options.my_argument)