0

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.

ahoyt41
  • 17
  • 1
  • 7
  • Hi! I dont understand what it is you are trying to do. Where should host/username/password come from? Do you or do you NOT want to hard code the host? That sentence in your question seems to contradict itself. – Cyberwiz Jul 23 '20 at 20:01
  • I just reread my post and realized that I mistyped. I have the host saved in a locust config file. My original question is if theres a way to specify arguments that will passed into '``on_test_start()``' and if not, where is host referenced so that I can pull it into my start function. – ahoyt41 Jul 27 '20 at 16:43

2 Answers2

2

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”], ...)
Cyberwiz
  • 11,027
  • 3
  • 20
  • 40
  • Thanks, this has been helpful. I was getting an error saying that ```environment.parsed_args``` doen't exist, but ```environment``` does have a host variable that i can use – ahoyt41 Jul 27 '20 at 17:27
  • Cool. It was supposed to be parsed_options, i have updated my answer – Cyberwiz Jul 27 '20 at 17:29
1

I can't comment on Cyberwiz's answer so I post a new answer, hope you don't mind.

I'm trying to do something similar to what you want to do, but I cannot get host url by doing environment.parsed_options["host"] , I got an error message which said 'Namespace' object is not subscriptable

Instead, I tried using environment.host and I got the host url successfully, hope this might help for those who meet the same problem as I did!

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