pythonlocustfasthttp

Locust FastResponse's failure attribute doesn't set request as failed in report


I have such task in locustfile:

class Tasks(FastHttpUser):
    headers = {'content-type': 'application/json', 'Connection': 'close'}

    @task
    def task(self):
        payload = "some data"
        try:
            with self.client.post("/endpoint", data=payload, headers=self.headers,
                                  name="request", catch_response=True) as response:
                result = json.loads(response.content)
                if len(result["result"]) == 0:
                    response.failure(result)
                    log.error(result)
        except (TypeError, JSONDecodeError) as err:
            response.failure(response.text)
            log.error(f'{type(err).__name__} because of : {response.status_code} - {response.text}')

I expected that if response will not pass statement or there will be some unexpected answer, this response will be marked as failed and be added to statistic as failure, but it isn't happen, I see only error in logs. Is it the right way to use failure attribute or it's better to use something different if I want to check response's content?


Solution

  • You need to do response.failure(...) inside the with-block. The way you are doing, the response has already been lost because of the unexpected termination of the with-block.

    Use something like this:

    with self.client.post("/endpoint", data=payload, headers=self.headers,
                            name="request", catch_response=True) as response:
        try:
            result = json.loads(response.content)
            if len(result["result"]) == 0:
                response.failure(result)
                log.error(result)
        except (TypeError, JSONDecodeError) as err:
            response.failure(response.text)
            log.error(f'{type(err).__name__} because of : {response.status_code} - {response.text}')