OiO.lk Community platform!

Oio.lk is an excellent forum for developers, providing a wide range of resources, discussions, and support for those in the developer community. Join oio.lk today to connect with like-minded professionals, share insights, and stay updated on the latest trends and technologies in the development field.
  You need to log in or register to access the solved answers to this problem.
  • You have reached the maximum number of guest views allowed
  • Please register below to remove this limitation

pytest assert message customization with variable introspection

  • Thread starter Thread starter Marc Tudurí
  • Start date Start date
M

Marc Tudurí

Guest
In the pytest documentation it says that you can customize the output message when an assert fails. I want to customize the assert message when testing a REST API method it returns an invalid status code:

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

Code:
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 of response.status_code so it's an int instead of a Response. 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 object Response.

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?
<p>In the <a href="https://docs.pytest.org/en/latest/assert.html#defining-your-own-assertion-comparison" rel="noreferrer">pytest documentation</a> it says that you can customize the output message when an <code>assert</code> fails. I want to customize the <code>assert</code> message when testing a REST API method it returns an invalid status code:</p>

<pre><code>def test_api_call(self, client):
response = client.get(reverse('api:my_api_call'))
assert response.status_code == 200
</code></pre>

<p>So I tried to put a piece of code like this in <code>conftest.py</code></p>

<pre><code>def pytest_assertrepr_compare(op, left, right):
if isinstance(left, rest_framework.response.Response):
return left.json()
</code></pre>

<p>But the problem is <code>left</code> is the actual value of <code>response.status_code</code> so it's an <code>int</code> instead of a <code>Response</code>. However the default output messages throws something like:</p>

<blockquote>
<p>E assert 400 == 201 E + where 400 = .status_code</p>
</blockquote>

<p>Saying that the error 400 comes from an attribute <code>status_code</code> of a object <code>Response</code>. </p>

<p>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?</p>
 
Top