Package: django-3t
Django 3TDjango 3T is a Django Template Testing Tool which aims to help developers to write better tests where the Django template engine is involved.Some of the things you can do with Django 3T include:Ensure a specifictemplate,nodeorblockis renderedEnsure a specifictemplate,nodeorblockis rendered a specific amount of timesEnsure a specifictemplateis rendered with a specific context subsetEnsure a specificnodeis rendered with specific argumentsEnsure a specifictemplate,nodeorblockresults in a specific content or includes itInstallationpip install django-3tUsageDjango 3T uses thewatch_templatescontext manager to intercept template and node renderings.Suppose your project implements the following template calledhomepage.html:{% load say_hello from project_tags %}<h1>The most useful website ever</h1>{% say_hello request.user %}A test that makes sure your template and template tag are rendered correctly would roughly look like this:fromdjango.contrib.authimportget_user_modelfromdjango.testimportClient# 1. Import the context managerfromd3t.watcherimportwatch_templatesUser=get_user_model()deftest_homepage():user=User.objects.get(username='Billy')client=Client()client.force_login(user)# 2. Wrap the code where the rendering happenswithwatch_templatesasrendered:client.get('/')# 3. Assert!assertrendered.template('homepage.html')assertrendered.node('say_hello').with_arguments(user)The first assertion makes sure the templatehomepage.htmlwas rendered, the second assertion makes sure the template tagsay_hellowas rendered and it was done usinguseras argument.For a comprehensive list of available methods, check theAPI section.APIYou can check that a template, node or block has been rendered and that it did under specific conditionsTemplate APICheck that it has been renderedrendered.template('template-name.html')Check that it has been rendered with a specific context subsetrendered.template('template-name.html').with_context({'answer':42})Check that it has been rendered and the output contains a specific stringrendered.template('template-name.html').contains('content')Check that it has been rendered and the output equals a specific stringrendered.template('template-name.html').equals('full content')Node APICheck that it has been renderedrendered.node('node_name')Check that it has been rendered with specific argumentsrendered.node('node_name').with_arguments(42,type='answer')Check that it has been rendered and the output contains a specific stringrendered.node('node_name').contains('content')Check that it has been rendered and the output equals a specific stringrendered.node('node_name').equals('full content')Block APICheck that it has been renderedrendered.block('block-name')Check that it has been rendered and the output contains a specific stringrendered.block('block-name').contains('content')Check that it has been rendered and the output equals a specific stringrendered.block('block-name').equals('full content')Handling multilpe renderingsA template, node or block could be rendered any number of times, Django 3T allows you to take control of this giving support for thenotoperator and for thelen,allandanybuilt-in functions:Check that a template/node/block has not been renderednotrendered.template('template-name.html')Check that a template/node/block has been rendered a specific amount of timeslen(rendered.node('node_name'))==3Check that all the template/node/block renderings happened under a specific conditionall(rendered.block('block-name').contains('content'))Check that any of the template/node/block renderings happened under a specific conditionany(rendered.template('template-name.html').equals('specific content'))Signalstemplate_renderedd3t.signals.template_renderedThis is sent immediately after a template is rendered.Arguments sent with this signal:senderTheTemplateclass.instanceThe actual template instance being rendered.contextThe context used to render the template.resultThe resulting rendered output.node_renderedd3t.signals.node_renderedThis is sent immediately after a node is rendered.Arguments sent with this signal:senderTheNodeclass.instanceThe actual node instance being rendered.resultThe resulting rendered output.