问题
I would like to know if there is any way with which i can send host value as parameter. I am aware that locust provides a paramter, "--host" which can help here but it's not working for me. This is my code implementation -
class NcsoTest(TaskSet):
REQ_HEADER = {
"Accept": "application/json",
"Accept-Encoding": "gzip, deflate",
"Connection": "keep-alive",
"Content-Length": "860",
"Content-Type": "application/json",
"User-Agent": "python-requests/2.21.0",
}
@task(1)
def send_post_request(self):
response = self.client.post("/api/v2/services", data=Singleton.json_body, headers=NcsoTest.REQ_HEADER)
print response
class NcsoLoad(HttpLocust):
max_wait = 300
min_wait = 300
sleep_time = 10
task_set = NcsoTest
I am using this command to run locust. pipenv run locust -f testsuite/playground/locust_create_ics_host_flow.py --master --no-web --clients=1 --hatch-rate=1 --host=https://10.247.123.172 --run-time=1m
I tried multiple approaches but host value is not propagated and picked up in self.client.post.
Can anyone help me with this?
回答1:
There are three options to accomplish what you are asking about. The first option is to change the host entry parameter by removing the equal sign.
from
--host=https://10.247.123.172
to
--host https://10.247.123.172
In the second option, edit your test file and add a host entry as listed below.
testsuite/playground/locust_create_ics_host_flow.py
class NcsoLoad(HttpLocust):
host = "https://10.247.123.172"
max_wait = 300
min_wait = 300
sleep_time = 10
task_set = NcsoTest
Use either option, but not both at the same time. It may cause confusion on down the road. The host entries for options one and two are required to display the host site at the web UI while running tests. Not using a host entry will produce a "None" value at the web UI. Not a big deal unless working with customers while testing or using the "--no-web" option in your original post.
The last option is really cool and provides more flexibility when writing test cases. More information is provided in the link below.
# Support for tests that use multiple hosts
https://github.com/locustio/locust/issues/150
One more thing to consider. Remove all of the "=" since they're not required in each parameter.
from
locust -f testsuite/playground/locust_create_ics_host_flow.py --master --no-web --clients=1 --hatch-rate=1 --host=https://10.247.123.172 --run-time=1m
to
locust --locustfile testsuite/playground/locust_create_ics_host_flow.py --master --no-web --clients 1 --hatch-rate 1 --host https://10.247.123.172 --run-time 1m
From the "The Zen of Python, by Tim Peters"
>>> import this
Explicit is better than implicit.
Hope this information is helpful.
Regards,
回答2:
I am able to send host value to my program by using host parameter from outside the script. Here are the two commands I used through shell script and passed values for :
./run_locust_host.sh --locust_file testsuite/host/locust_host_flow_task.py --host https://10.123.123.123 --min_wait_time 300 --max_wait_time 300 --num_clients 1 --hatch_rate 1 --test_time 2m
this script parses all the details and assigns the value to respective locust parameter.
Inside the script (py file), Its able to pick up host value and execute below post request.
response = self.client.post("/api/v2/services", data=Singleton.json_body, headers=NcsoTest.REQ_HEADER, verify=False)
My earlier request was working but failing since I did not specify "verify=False" in post request which led me to believe that host value is not propagated.
回答3:
You can define a config.py file like this:
class EnvSettings(object):
host_url = 'http://test.com/'
Then from your locust class, you can import
host = EnvSettings.host_url
来源:https://stackoverflow.com/questions/59085387/is-there-any-way-where-i-can-send-host-value-as-a-parameter-in-locust