pytest assert message customization with variable introspection
you could use Python built-in capability to show custom exception message:
assert response.status_code == 200, f"My custom msg: actual status code {response.status_code}"
Or you can built a helper assert functions:
def assert_status(response, status=200): # you can assert other status codes too
assert response.status_code == status, \
f"Expected {status}. Actual status {response.status_code}. Response text {response.text}"
# here is how you'd use it
def test_api_call(self, client):
response = client.get(reverse('api:my_api_call'))
assert_status(response)
also checkout: https://wiki.python.org/moin/UsingAssertionsEffectively
Related videos on Youtube
Marc Tudurí
Updated on June 07, 2022Comments
-
Marc Tudurí almost 2 years
In the pytest documentation it says that you can customize the output message when an
assert
fails. I want to customize theassert
message when testing a REST API method it returns an invalid status code:def test_api_call(self, client): response = client.get(reverse('api:my_api_call')) assert response.status_code == 200
So I tried to put a piece of code like this in
conftest.py
def pytest_assertrepr_compare(op, left, right): if isinstance(left, rest_framework.response.Response): return left.json()
But the problem is
left
is the actual value ofresponse.status_code
so it's anint
instead of aResponse
. However the default output messages throws something like:E assert 400 == 201 E + where 400 = .status_code
Saying that the error 400 comes from an attribute
status_code
of a objectResponse
.My point is that there is a kind of introspection of the variable being evaluated. So, how can I customize the assert error message in a comfortable way to get a similar output to example posted above?
-
Marc Tudurí almost 7 yearsThanks Dmitry. I know the both solutions that you propose and sure that both will work. However I was looking for a fancy solution where you can still use
assert
and you won't need to write more code. -
Dmitry Tokarev almost 7 yearsahh, sorry, didn't get your question intent
-
tahoe almost 5 yearsThe first example in the above response is exactly what I was looking for!