with pytest.raises(ValueError):
client.get('/endpoint')
This code tests a Flask application.
The ValueError is not caught, and eventually crashes the test.
Any idea what the problem could be?
Just a hunch: Earlier in your test suite, you have code like except TypeError, ValueError:
This is the old-style except syntax kicking in, with the effect of writing except TypeError as ValueError, which changes the name ValueError to an instance of TypeError. The correct code for catching multiple exception types is except (TypeError, ValueError):
Please paste/attach a full output
As you can see, it outputs Failed: DID NOT RAISE. But the ValueError propagates and crashes the test.
鈫怺1m with pytest.raises(ValueError):鈫怺0m
鈫怺1m> client.get('/tuple5')鈫怺0m
鈫怺1m鈫怺31mE Failed: DID NOT RAISE鈫怺0m
tests\test_basic.py:977: Failed
---------------------------- Captured stderr call -----------------------------
[2016-01-02 16:15:58,851] ERROR in app: Exception on /tuple5 [GET]
Traceback (most recent call last):
File "c:\users\owner\desktop\flask\flask\app.py", line 1994, in wsgi_app
response = self.full_dispatch_request()
File "c:\users\owner\desktop\flask\flask\app.py", line 1621, in full_dispatch_
request
response = self.make_response(rv)
File "c:\users\owner\desktop\flask\flask\app.py", line 1719, in make_response
return self._make_response_from_tuple(view_func_rv)
File "c:\users\owner\desktop\flask\flask\app.py", line 1724, in _make_response
_from_tuple
response_body, status, headers = self._extract_response_parts_from_tuple(ret
val_from_view_func)
File "c:\users\owner\desktop\flask\flask\app.py", line 1738, in _extract_respo
nse_parts_from_tuple
raise ValueError('Tuples returned from view functions must be composed as on
e '
ValueError: Tuples returned from view functions must be composed as one of the f
ollowing options: (response_body, status, headers), (response_body, status) or (
response_body, headers).
鈫怺1m鈫怺31m============== 1 failed, 257 passed, 11 skipped in 7.67 seconds =======
========鈫怺0m
Thats normal test client behaviour
It makes a 500 response
Unless you configure it to pass trough exceptions
Sorry I don't understand what you mean.
client.get('/endpoint') raises a ValueError. I assume pytest.raises(ValueError) should catch the error and pass the test. However it does two weird things:
ValueError was not raised (Failed: DID NOT RAISE).ValueError propagate and crash the test.How is this possible? And how can I simply assert that the exception is raised (this is what I'm testing for).
It looks like the exception is being logged to stderr, not being raised at all. Try it manually via:
try:
client.get('/endpoint')
except ValueError:
pass
else:
raise Exception("Value error not found")
And you should see the same result, no?
The test client logs the exception and returns a response with the 500 status
Please configure the test client to pass trough exceptions when you want to catch them
Closing as invalid, in the PR where he experienced the issue he already fixed his api misuse
Could you link to that pull request? Or outline how to configure the test client to pass through exceptions?
Most helpful comment
Could you link to that pull request? Or outline how to configure the test client to pass through exceptions?