From 2133f3157eff853329bafb7fda74c3c8fb4eae42 Mon Sep 17 00:00:00 2001 From: Aymeric Augustin Date: Mon, 26 Jan 2015 21:57:10 +0100 Subject: Fixed #24168 -- Allowed selecting a template engine in a few APIs. Specifically in rendering shortcuts, template responses, and class-based views that return template responses. Also added a test for render_to_response(status=...) which was missing from fdbfc980. Thanks Tim and Carl for the review. --- django/test/utils.py | 24 +++++++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-) (limited to 'django/test') diff --git a/django/test/utils.py b/django/test/utils.py index 1991602293..f0d8993a40 100644 --- a/django/test/utils.py +++ b/django/test/utils.py @@ -3,7 +3,7 @@ import logging import re import sys import time -from unittest import skipUnless +from unittest import skipIf, skipUnless import warnings from functools import wraps from xml.dom.minidom import parseString, Node @@ -20,6 +20,11 @@ from django.utils import six from django.utils.encoding import force_str from django.utils.translation import deactivate +try: + import jinja2 +except ImportError: + jinja2 = None + __all__ = ( 'Approximate', 'ContextList', 'get_runner', @@ -573,3 +578,20 @@ def freeze_time(t): yield finally: time.time = _real_time + + +def require_jinja2(test_func): + """ + Decorator to enable a Jinja2 template engine in addition to the regular + Django template engine for a test or skip it if Jinja2 isn't available. + """ + test_func = skipIf(jinja2 is None, "this test requires jinja2")(test_func) + test_func = override_settings(TEMPLATES=[{ + 'BACKEND': 'django.template.backends.django.DjangoTemplates', + 'APP_DIRS': True, + }, { + 'BACKEND': 'django.template.backends.jinja2.Jinja2', + 'APP_DIRS': True, + 'OPTIONS': {'keep_trailing_newline': True}, + }])(test_func) + return test_func -- cgit v1.3