Tuesday, March 17, 2015

Re: Testing template context processors

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1

iQIcBAEBCAAGBQJVCF17AAoJEC0ft5FqUuEh0bAP/iw5Bg2mjdOy3hfl61/HjvvA
QFtwVBTdPWFyGbdkk5zVJ7890P+zzrvE6kzj0oThFSXyQWW3LHk4FYIjxv/W5Kd4
F0psJgMDVVqc2plK0zqoJqZ/FXdsf1cB4/xfaG4q5YTiPCUiAlQ2orz3B/Gu2Y7h
Xv1Oqy0JBp63jIDlOXvjFJhSTowlwmBULVMoiHIMPdc6cslfvNn1Zt5CQXmCCbwx
K/SL4yQ+zluElNmj5U7195uB5zQ9EYcEKhaYosYAxs3X4uSUSyb89ME1GMI4SZNk
NIdtb3GDKuzFR4cI8J2wQAs3hlVdSl07gO2hDqlYXXuLOT+DYME+8qbvl/DVWCzk
e5zZOrYBHBSaAQ9zFuCHWJRDJ+U65nHq4yDC+HIYEOH00r2BFfW8sqO8CS5o23c+
ZTi18KSCIuCPMNuXSyaplQRbagbtUlT+ckgoVQoPxunHnfk42lOE3uzq4f3Efgkk
7yovW+g6IIXHY4eot6tAQsFskV8Srh30/2xNB2+oivpsGOWkXfDpc37g6Z0aac9F
HrEegbup4FvIAt+T8Ha8jiY/H7b9HJ78KgsaY5m0Ucqd//5gHvCRb/0tBt/4pGyS
ldRqarOfsn+M2G9Ph4q53/pD4QmkFKnvuccOsccubV8puU5i9MHM7pDwgUOYyv9n
LMEUgoyEIvIaC63DHfWu
=6deK
-----END PGP SIGNATURE-----
Hi Gergely,

On 03/17/2015 10:52 AM, Gergely Polonkai wrote:
> I wrote a context processor which makes some global variables of mine
> available for templates. I cannot find a good (cough… any) article on
> how to test such processors. Could you give me some directions on where
> to start with writing one?

Writing a unit test for a context processor is quite simple. Construct a
fake request (you can use RequestFactory, though if your context
processor only accesses one or two attributes of the request, it might
be simpler to just build a custom fake or use a library like pretend
[1]), then call the context processor function in your test, passing in
the fake request, and assert that the returned dictionary contains what
you expect.

If your templates require the values from the context processor in order
to render correctly, you can also write a higher-level integration or
functional test by using the test client to access a view that renders a
template that needs the context processor, and then assert the right
value is found in the rendered HTML.

Carl

[1] https://pypi.python.org/pypi/pretend

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/55085D7A.5040007%40oddbird.net.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment