jeudi 12 avril 2018

How to write a Django test that checks whether an error occurs on a page?

I'm currently debugging a pull request that has made a certain page error out:

enter image description here

The view is subclassed from Django's generic UpdateView, and corresponds to the check-ins tab of a URL named 'family' in the dashboard app:

url(
    r'^families/(?P<pk>[0-9]+)/'
    r'(?P<tab>case-management|scheduling|family-demographics|activation-payment|check-ins)$',
    views.FamilyUpdate.as_view(),
    name='family'),

As a first step, I'd like to write a test which captures what I see, namely, an error occurring, so that after I fix it I can be sure it never happens again.

How can achieve this? I've tried the following test case,

from django.test import TestCase
from django.urls import reverse
from lucy_web.test_factories import FamilyFactory, UserFactory


class TestDashboardNavigation(TestCase):
    '''Test class to simply navigate to pages and click the links, making sure no errors occur'''
    def setUp(self):
        self.client.force_login(UserFactory(is_superuser=True))

    def test_family_navigation(self):
        family = FamilyFactory()
        for tab in ["case-management", "scheduling", "family-demographics", "activation-payment", "check-ins"]:
            url = reverse('dashboard:family', kwargs={'pk': family.id, 'tab': tab})
            response = self.client.get(url)
            print(response.status_code)

where FamilyFactory is a factory_boy test fixture which creates a Family object (the underlying model of the UpdateView), but this test simply passes:

(venv) Kurts-MacBook-Pro-2:lucy-web kurtpeek$ python manage.py test dashboard.tests.test_navigation
Creating test database for alias 'default'...
System check identified no issues (0 silenced).
302
302
302
302
302
.
----------------------------------------------------------------------
Ran 1 test in 1.294s

OK

How would I catch errors like this occurring on a page?

Aucun commentaire:

Enregistrer un commentaire