I use locust under python3.5.3 to learn to do some performance testing demo, some code as below.
class UserBehavior(TaskSet):
# Execute before any task
def on_start(self):
pass
@task(1)
def api_query(self):
with self.client.get("/api/nodes/show.json?name=python") as response:
if response.content == "":
response.failure("No data")
if json.loads(response.content.decode())["id"] != "90":
response.failure("Got wrong response, id=" + response.content.decode())
else:
response.success("Verify Ok!")
when i run locust, i get error message:
cent call last):
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/locust/core.py", line 271, in run
self.execute_next_task()
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/locust/core.py", line 297, in execute_next_task
self.execute_task(task["callable"], task["args"], *task["kwargs"])
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/locust/core.py", line 309, in execute_task
task(self, args, *kwargs)
File "/Users/xiongyang/Desktop/个人/learnning/python3/rfinex_locust.py", line 29, in login
response.failure("Got wrong response, id=" + response.content.decode())
AttributeError: 'Response' object has no attribute 'failure'
TBD
TBD
TBD - example code appreciated
I am new to locust, maybe this is a known issue or just I used locust in a wrong way, please some could help me solve this issue? Thx a lot.
@YannickXiong you need to catch the response to be able to use it in a with statement, like so:
@task(1)
def api_query(self):
with self.client.get("/api/nodes/show.json?name=python",
catch_response=True) as response:
…
@mpaddle is correct. In order to be able to use the response object as a context manager (with the with statement), you have to set the catch_response keyword argument to True.
Most helpful comment
@YannickXiong you need to catch the response to be able to use it in a
withstatement, like so: