Locust: 'Response' object has no attribute 'failure' in python3.5.3

Created on 27 Oct 2017  ·  3Comments  ·  Source: locustio/locust

Description of issue / feature request

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'

Expected behavior

TBD

Actual behavior

TBD

Environment settings (for bug reports)

Steps to reproduce (for bug reports)

TBD - example code appreciated

Most helpful comment

@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:
        …

All 3 comments

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.

Was this page helpful?
0 / 5 - 0 ratings