Django eXtra Test Cases¶
Django’s test cases focus on test cases for use with a database.
This is excellent as it is the hardest thing to do right. Yet, when creating (reusable) apps that do not have any models or testing views that do not use any models, you often find yourself doing the same thing with SimpleTestCase.
This issue is the focus of Django XTC.
TemplateTestCase¶
The core test case for testing templates. Requires a template test directory which can be set on the class level and will fall back to TEMPLATE_TEST_DIR using Django’s settings.
Below this directory, at least two subdirectories are used:
src
: The template codeexpect
: The expected output of that template code
actual
: (optional) Used if the output is to be saved for post-mortem- analysis.
At the core is the test method assertTemplateEqual, which compares the expected template code with the actual. The template context (the variables) can be manipulated using the template_context attribute.
Installation¶
The project is available via Pypi:
$ pip install django-xtc
Alternatively, the project can be installed by downloading the repository and running the setup program:
$ git clone https://github.com/melvyn-sopacua/django_xtc.git
$ cd django_xtc
$ python setup.py install
Quick-start¶
The primary test case is TemplateTestCase:
from xtc import TemplateTestCase
class TestStaticHomePage(TemplateTestCase):
template_test_dir = '/path/to/test/templates'
def test_simple(self):
self.assertTemplateEqual('home.html')
This will render the template code in /path/to/test/templates/src/home.html
and compare it with the output in /path/to/test/templates/expect/home.html
.