English 中文(简体)
How to test custom template tags in Django?
原标题:

I m adding a set of template tags to a Django application and I m not sure how to test them. I ve used them in my templates and they seem to be working but I was looking for something more formal. The main logic is done in the models/model managers and has been tested. The tags simply retrieve data and store it in a context variable such as

{% views_for_object widget as views %}
"""
Retrieves the number of views and stores them in a context variable.
"""
# or
{% most_viewed_for_model main.model_name as viewed_models %}
"""
Retrieves the ViewTrackers for the most viewed instances of the given model.
"""

So my question is do you typically test your template tags and if you do how do you do it?

最佳回答

This is a short passage of one of my test files, where self.render_template a simple helper method in the TestCase is:

    rendered = self.render_template(
         {% load templatequery %} 
         {% displayquery django_templatequery.KeyValue all() with "list.html" %} 
    )
    self.assertEqual(rendered,"foo=0
bar=50
spam=100
egg=200
")

    self.assertRaises(
        template.TemplateSyntaxError,
        self.render_template,
         {% load templatequery %} 
         {% displayquery django_templatequery.KeyValue all() notwith "list.html" %} 
    )

It is very basic and uses blackbox testing. It just takes a string as template source, renders it and checks if the output equals the expected string.

The render_template method is quite simplistic:

from django.template import Context, Template

class MyTest(TestCase):
    def render_template(self, string, context=None):
        context = context or {}
        context = Context(context)
        return Template(string).render(context)
问题回答

You guys got me on the right track. It s possible to check that the context was correctly changed after the render:

class TemplateTagsTestCase(unittest.TestCase):        
    def setUp(self):    
        self.obj = TestObject.objects.create(title= Obj a )

    def testViewsForOjbect(self):
        ViewTracker.add_view_for(self.obj)
        t = Template( {% load my_tags %}{% views_for_object obj as views %} )
        c = Context({"obj": self.obj})
        t.render(c)
        self.assertEqual(c[ views ], 1)

A good example of how to test template tags test of flatpage templatetags

Strings can be rendered as templates, so you could write a test that includes a simple template using your templatetag as a string and just make sure it renders correctly given a certain context.

When I was testing my template tags, I would have the tag, itself, return a string containing the text or dict I was working with ... sort of along the lines of the other suggestion.

Since tags can modify the context and/or return a string to be rendered -- I found it was fastest just to view the rendered string.

Instead of:

return   

Have it:

return str(my_data_that_I_am_testing)

Until you are happy.





相关问题
How to get two random records with Django

How do I get two distinct random records using Django? I ve seen questions about how to get one but I need to get two random records and they must differ.

Moving (very old) Zope/Plone Site to Django

I am ask to move data from a (now offline) site driven by Plone to a new Django site. These are the version informations I have: Zope Version (unreleased version, python 2.1.3 ) Python Version 2.1....

Can Django models use MySQL functions?

Is there a way to force Django models to pass a field to a MySQL function every time the model data is read or loaded? To clarify what I mean in SQL, I want the Django model to produce something like ...

Flexible pagination in Django

I d like to implement pagination such that I can allow the user to choose the number of records per page such as 10, 25, 50 etc. How should I go about this? Is there an app I can add onto my project ...

is it convenient to urlencode all next parameters? - django

While writing code, it is pretty common to request a page with an appended "next" query string argument. For instance, in the following template code next points back to the page the user is on: &...

Pragmatically adding give-aways/freebies to an online store

Our business currently has an online store and recently we ve been offering free specials to our customers. Right now, we simply display the special and give the buyer a notice stating we will add the ...

热门标签