Skip to main content
Pre-release

This release is a pre-release and may not be stable for production use.

PyPI Version Travis Build Code Coverage License

Django 3T

Django 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 specific template, node or block is rendered

  • Ensure a specific template, node or block is rendered a specific amount of times

  • Ensure a specific template is rendered with a specific context subset

  • Ensure a specific node is rendered with specific arguments

  • Ensure a specific template, node or block results in a specific content or includes it

Installation

pip install django-3t

Usage

Django 3T uses the watch_templates context manager to intercept template and node renderings.

Suppose your project implements the following template called homepage.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:

from django.contrib.auth import get_user_model
from django.test import Client

# 1. Import the context manager
from d3t.watcher import watch_templates

User = get_user_model()

def test_homepage():
    user = User.objects.get(username='Billy')
    client = Client()
    client.force_login(user)

    # 2. Wrap the code where the rendering happens
    with watch_templates as rendered:
        client.get('/')

    # 3. Assert!
    assert rendered.template('homepage.html')
    assert rendered.node('say_hello').with_arguments(user)

The first assertion makes sure the template homepage.html was rendered, the second assertion makes sure the template tag say_hello was rendered and it was done using user as argument.

For a comprehensive list of available methods, check the API section.

API

You can check that a template, node or block has been rendered and that it did under specific conditions

Template API

Check that it has been rendered

rendered.template('template-name.html')

Check that it has been rendered with a specific context subset

rendered.template('template-name.html').with_context({'answer': 42})

Check that it has been rendered and the output contains a specific string

rendered.template('template-name.html').contains('content')

Check that it has been rendered and the output equals a specific string

rendered.template('template-name.html').equals('full content')

Node API

Check that it has been rendered

rendered.node('node_name')

Check that it has been rendered with specific arguments

rendered.node('node_name').with_arguments(42, type='answer')

Check that it has been rendered and the output contains a specific string

rendered.node('node_name').contains('content')

Check that it has been rendered and the output equals a specific string

rendered.node('node_name').equals('full content')

Block API

Check that it has been rendered

rendered.block('block-name')

Check that it has been rendered and the output contains a specific string

rendered.block('block-name').contains('content')

Check that it has been rendered and the output equals a specific string

rendered.block('block-name').equals('full content')

Handling multilpe renderings

A template, node or block could be rendered any number of times, Django 3T allows you to take control of this giving support for the not operator and for the len, all and any built-in functions:

Check that a template/node/block has not been rendered

not rendered.template('template-name.html')

Check that a template/node/block has been rendered a specific amount of times

len(rendered.node('node_name')) == 3

Check that all the template/node/block renderings happened under a specific condition

all(rendered.block('block-name').contains('content'))

Check that any of the template/node/block renderings happened under a specific condition

any(rendered.template('template-name.html').equals('specific content'))

Signals

template_rendered

d3t.signals.template_rendered

This is sent immediately after a template is rendered.

Arguments sent with this signal:

  • sender
    The Template class.
  • instance
    The actual template instance being rendered.
  • context
    The context used to render the template.
  • result
    The resulting rendered output.

node_rendered

d3t.signals.node_rendered

This is sent immediately after a node is rendered.

Arguments sent with this signal:

  • sender
    The Node class.
  • instance
    The actual node instance being rendered.
  • result
    The resulting rendered output.

Metadata

Release files for django-3t 1.0.0a1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for django-3t 1.0.0a1
File Size Uploaded Provenance Actions
django-3t-1.0.0a1.tar.gz 11.0 kB Without provenance Details for django-3t-1.0.0a1.tar.gz

Release files / django-3t-1.0.0a1.tar.gz

Download URL django-3t-1.0.0a1.tar.gz
Size 11.0 kB
Tags Source
SHA-256 checksum
How to use checksums
de9d61e54ad62d7afbb7370372d59e40cc6b7c460749efd9d73a4aa51bbaad63
BLAKE2b-256 checksum
How to use checksums
811c225d69f240a61741ccbbc0cd470c50277fcf37a8c47b227c96852df8d650
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/1.15.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/44.0.0 requests-toolbelt/0.9.1 tqdm/4.42.0 CPython/2.7.15

Security for django-3t 1.0.0a1

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release
Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page