mercredi 2 septembre 2020

Django Test message passed in with Http404

I'm trying to check if my app passes a message alongside my Http404 call. However I've not been able to access that message inside the tests, only via hacking manually in shell.

my_app.views.py:

from django.http import Http404
def index(request):
    raise Http404("My message")

Then in this app's test file I call:

from django.test import TestCase
from django.urls import reverse

class AppIndexView(TestCase):
    def test_index_view(self):
        response = self.client.get(reverse("my_app:index"))

        self.assertEqual(response.status_code, 404)
        # This checks

        self.assertEqual(response.context["reason"], "My message"
        # This gives: KeyError: 'reason'
        # However if I manually trace these steps I can access this key.

        self.assertContains(response, "My Message")
        # This gives: AssertionError: 404 != 200 : Couldn't
        # retrieve content: Response code was 404 (expected 200)
        # Even though the status code test checks.

I also have tried various version to get that message with response.exception, response.context.exception etc. as detailed in this question

If I execute in django's shell I can access that message:

>>> from django.test import Client
>>> from django.urls import reverse
>>> from django.test.utils import setup_test_environment
>>> setup_test_environment()
>>> client=Client()
>>> response = client.get(reverse("my_app:index"))
>>> response.context["reason"]
'My Message'

How can I get access to this message in my tests.py?

Aucun commentaire:

Enregistrer un commentaire