Search code examples
pythonlocust

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


I'm trying to run load tests with locust but I need to run some API calls before I start spawning workers. I already pass my user credentials as environment variables but I don't want to have to hard-code the host within the locustfile. I'm ideally looking to have something that looks like

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

Where init will take care of all of the initial API calls.


Solution

  • The first parameter, environment, to the init function contains locust settings in the parsed_options dict. Try getting host from there.

    Something like (untested)

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