diff --git a/locust/clients.py b/locust/clients.py index 6d9f5df79a..8228e96d95 100644 --- a/locust/clients.py +++ b/locust/clients.py @@ -295,3 +295,20 @@ def failure(self, exc): if not isinstance(exc, Exception): exc = CatchResponseError(exc) self._manual_result = exc + + +# Monkey patch Response class to give some guidance +def _success(self): + raise LocustError( + "If you want to change the state of the request, you must pass catch_response=True. See http://docs.locust.io/en/stable/writing-a-locustfile.html#validating-responses" + ) + + +def _failure(self): + raise LocustError( + "If you want to change the state of the request, you must pass catch_response=True. See http://docs.locust.io/en/stable/writing-a-locustfile.html#validating-responses" + ) + + +Response.success = _success +Response.failure = _failure diff --git a/locust/contrib/fasthttp.py b/locust/contrib/fasthttp.py index edb96bf712..e81722dbd7 100644 --- a/locust/contrib/fasthttp.py +++ b/locust/contrib/fasthttp.py @@ -370,12 +370,12 @@ def _content(self): return super()._content() def success(self): - raise Exception( + raise LocustError( "If you want to change the state of the request, you must pass catch_response=True. See http://docs.locust.io/en/stable/writing-a-locustfile.html#validating-responses" ) def failure(self): - raise Exception( + raise LocustError( "If you want to change the state of the request, you must pass catch_response=True. See http://docs.locust.io/en/stable/writing-a-locustfile.html#validating-responses" ) diff --git a/locust/test/test_fasthttp.py b/locust/test/test_fasthttp.py index f496050dda..ae92be4068 100644 --- a/locust/test/test_fasthttp.py +++ b/locust/test/test_fasthttp.py @@ -589,7 +589,7 @@ def on_failure(**kw): def test_missing_catch_response_true(self): # incorrect usage, missing catch_response=True with self.user.client.get("/fail") as resp: - self.assertRaises(Exception, resp.success) + self.assertRaises(LocustError, resp.success) class TestFastHttpSsl(LocustTestCase): diff --git a/locust/test/test_http.py b/locust/test/test_http.py index 075866dc00..fbdf049f2d 100644 --- a/locust/test/test_http.py +++ b/locust/test/test_http.py @@ -273,6 +273,12 @@ def test_catch_response_missing_with_block(self): self.assertRaises(LocustError, r.success) self.assertRaises(LocustError, r.failure, "") + def test_missing_catch_response_true(self): + s = self.get_client() + # incorrect usage, missing catch_response=True + with s.get("/fail") as resp: + self.assertRaises(LocustError, resp.success) + def test_user_context(self): class TestUser(HttpUser): host = f"http://127.0.0.1:{self.port}"