Test Utils¶
setup_test_app()
¶
New in version 0.7.
- djem.utils.tests.setup_test_app(package, label=None)[source]¶
Setup a Django test app for the provided package to allow test-only models to be used.
This function should be called from
myapp.tests.__init__
like so:setup_test_app(__package__)
This will create an app with the label “myapp_tests”. If a specific app label is required, it can be provided explicitly:
setup_test_app(__package__, 'mytests')
Using either of the above, models can be placed in
myapp.tests.models
and be discovered and used just like regular models by the test suite. As long asmyapp.tests
is not imported by anything that forms part of the standard Django runtime environment, these models will not be picked up in that environment, and will be isolated to the test suite only.This solution is adapted from Simon Charette’s comment on Django ticket #7835.
MessagingRequestFactory
¶
New in version 0.6.
- class djem.utils.tests.MessagingRequestFactory[source]¶
An extension of Django’s
RequestFactory
helper for tests that enables the use of the messages framework within the generated request. It does not use the standard message storage backend (as per theMESSAGE_STORAGE
setting), but rather a memory-only backend that does not involve the use of sessions, cookies or any other means of persistent storage of the messages. Thus, messages need to be read in the same request they were added, or they will be lost.It is used in the same way as
RequestFactory
:from django.contrib.auth.models import User from django.contrib import messages from django.test import TestCase from djem.utils.tests import MessagingRequestFactory # Views expected to set messages from .views import MyView, my_view class SimpleTest(TestCase): def setUp(self): self.factory = MessagingRequestFactory() self.user = User.objects.create_user( username='test.user', email='test@…', password='top_secret' ) def test_details(self): request = self.factory.get('/customer/details') # Recall that middleware are not supported. You can simulate a # logged-in user by setting request.user manually. request.user = self.user # Test my_view() as if it were deployed at /customer/details response = my_view(request) # Use this syntax for class-based views response = MyView.as_view()(request) self.assertEqual(response.status_code, 200) # Test the expected message was set message_list = list(messages.get_messages(request)) self.assertEqual(len(message_list), 1) self.assertEqual(message_list[0].message, 'An error occurred.')
TemplateRendererMixin
¶
New in version 0.6.
- class djem.utils.tests.TemplateRendererMixin[source]¶
A mixin for
TestCase
classes whose tests render templates from strings (as opposed to rendering them from files), using the Django template engine.- render_template(template_string, context, request=None, flatten=True)[source]¶
Render the given string as a template, with the given context and request (if provided).
If
request
is NOT provided, and aself.user
attribute is available on theTestCase
, a “user” variable will be automatically added to the context.The rendered output will be stripped of any leading or trailing whitespace, and can optionally have excess whitespace “flattened” by passing the
flatten
argument as True (the default). Flattening removes ALL whitespace from between HTML tags and compresses all other whitespace down to a single space.- Parameters
template_string – The string to render as a template.
context – The context with which to render the template.
request – The
HttpRequest
with which to render the template context.flatten – True to “flatten” the rendered output (default), False to return the output with all internal whitespace intact.
- Returns
The rendered template output.
from django.contrib.auth.models import User from django.test import TestCase from djem.utils.tests import TemplateRendererMixin class SomeTestCase(TemplateRendererMixin, TestCase): def setUp(self): self.user = self.user = User.objects.create_user( username='test.user', email='test@…', password='top_secret' ) def test_something(self): template_string = ( '{% if something %}' ' <p>' ' The user is: {{ user.username }}' ' </p>' '{% endif %}' ) output = self.render_template(template_string, { 'something': True }) self.assertEqual(output, '<p> The user is: test.user </p>')