Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Testing a custom Django template filter

I have a custom template filter I created under project/app/templatetags.

I want to add some regression tests for some bugs I just found. How would I go about doing so?

like image 999
Belmin Fernandez Avatar asked Dec 21 '10 03:12

Belmin Fernandez


People also ask

How do I perform query filtering in Django templates?

The easiest is to do the filtering, then pass the result to render_to_response . Or you could write a method in your model so that you can say {% for object in data. filtered_set %} . Finally, you could write your own template tag, although in this specific case I would advise against that.

How do I use custom template tags in Django?

Create a custom template tagUnder the application directory, create the templatetags package (it should contain the __init__.py file). For example, Django/DjangoApp/templatetags. In the templatetags package, create a . py file, for example my_custom_tags, and add some code to it to declare a custom tag.

How do I test a Django project?

The preferred way to write tests in Django is using the unittest module built-in to the Python standard library. This is covered in detail in the Writing and running tests document. You can also use any other Python test framework; Django provides an API and tools for that kind of integration.

How do I test my Django site?

Django comes with a small set of its own tools for writing tests, notably a test client and four provided test case classes. These classes rely on Python's unittest module and TestCase base class. The Django test client can be used to act like a dummy web browser and check views.


2 Answers

The easiest way to test a template filter is to test it as a regular function.

@register.filter decorator doesn't harm the underlying function, you can import the filter and use just it like if it is not decorated. This approach is useful for testing filter logic.

If you want to write more integration-style test then you should create a django Template instance and check if the output is correct (as shown in Gabriel's answer).

like image 140
Mikhail Korobov Avatar answered Sep 28 '22 03:09

Mikhail Korobov


Here's how I do it (extracted from my django-multiforloop):

from django.test import TestCase
from django.template import Context, Template

class TagTests(TestCase):
    def tag_test(self, template, context, output):
        t = Template('{% load multifor %}'+template)
        c = Context(context)
        self.assertEqual(t.render(c), output)
    def test_for_tag_multi(self):
        template = "{% for x in x_list; y in y_list %}{{ x }}:{{ y }}/{% endfor %}"
        context = {"x_list": ('one', 1, 'carrot'), "y_list": ('two', 2, 'orange')}
        output = u"one:two/1:2/carrot:orange/"
        self.tag_test(template, context, output)

This is fairly similar to how tests are laid out in Django's own test suite, but without relying on django's somewhat complicated testing machinery.

like image 26
Gabriel Grant Avatar answered Sep 28 '22 03:09

Gabriel Grant