Testing Your Tags

With native tags, the templatetag is simply just a function you define which means that testing the underlying functionality of your templatetag does not require going to your browser. Just pass in the appropriate arguments and test the output. Here is an example of unittesting a native tag which demonstrates how trivial it can be:

# tests.py
from django.test import TestCase
from native_tags import function

# Define or import your tags here

@function
def adder(x, y):
    return x + y

class AdderTest(TestCase):
    def test_adder(self):
        self.assertEqual(adder(1, 1), 2)

That is the full example above, but to add simple tests to a native function, you can use the test attribute passing in the arguments and expected result of the function call. Here is a shorter example that does the same thing as above:

def adder(x, y):
    return x + y
adder = function(adder, test={'args': (1, 1), 'result': 2})

Running ./manage.py test native_tags will test only your own tags in your project. This is useful for testing the sanity of your project and its applications and great to put in a CI job.

Test Application

The example_project folder in the source tree root is also useful for testing native tags itself. Run ./manage.py test app in that directory to run a test application on native tags.

Read the Docs v: latest
Versions
latest
Downloads
PDF
HTML
Epub
On Read the Docs
Project Home
Builds

Free document hosting provided by Read the Docs.