Skip to content

Commit 714935a

Browse files
committed
Add tests for Django templates
1 parent e28030d commit 714935a

File tree

4 files changed

+64
-2
lines changed

4 files changed

+64
-2
lines changed

pyproject.toml

+4
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,10 @@ source = "https://github.com/bittner/django-bootstrap-static"
4242

4343
[tool.pytest.ini_options]
4444
addopts = "--color=yes --doctest-modules --verbose"
45+
python_files = ["tests.py", "test_*.py", "*_tests.py"]
46+
pythonpath = ["tests"]
47+
DJANGO_SETTINGS_MODULE = "test_project.settings"
48+
FAIL_INVALID_TEMPLATE_VARS = true
4549

4650
[tool.ruff]
4751
extend-exclude = []

tests/test_project/settings.py

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
"""Minimal Django settings for test project."""
2+
3+
INSTALLED_APPS = [
4+
"django.contrib.staticfiles",
5+
"bootstrap",
6+
"fontawesome",
7+
]
8+
9+
TEMPLATES = [
10+
{
11+
"BACKEND": "django.template.backends.django.DjangoTemplates",
12+
"DIRS": [],
13+
"APP_DIRS": True,
14+
"OPTIONS": {
15+
"context_processors": [
16+
"django.template.context_processors.request",
17+
],
18+
},
19+
},
20+
]
21+
22+
STATIC_URL = "static"

tests/test_templates.py

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
"""Tests for Django templates."""
2+
3+
from django.template import Context, Template
4+
5+
template = """
6+
{% load static %}
7+
<head>
8+
<link rel="stylesheet" href="{% static 'bootstrap/css/bootstrap.min.css' %}">
9+
<script defer src="{% static 'fontawesome/js/all.min.js' %}"></script>
10+
</head>
11+
<body>
12+
...
13+
<script src="{% static 'bootstrap/js/jquery.min.js' %}"></script>
14+
<script src="{% static 'bootstrap/js/bootstrap.bundle.min.js' %}"></script>
15+
</body>
16+
"""
17+
18+
19+
def test_template():
20+
"""Importing the package should work as described in the README."""
21+
bootstrap_min_css = "bootstrap/css/bootstrap.min.css"
22+
bootstrap_min_js = "bootstrap/js/bootstrap.bundle.min.js"
23+
fontawesome_min_js = "fontawesome/js/all.min.js"
24+
jquery_min_js = "bootstrap/js/jquery.min.js"
25+
26+
c = Context()
27+
t = Template(template)
28+
html = t.render(c)
29+
30+
assert f'<link rel="stylesheet" href="/static/{bootstrap_min_css}">' in html
31+
assert f'<script defer src="/static/{fontawesome_min_js}"></script>' in html
32+
assert f'<script src="/static/{jquery_min_js}"></script>' in html
33+
assert f'<script src="/static/{bootstrap_min_js}"></script>' in html

tox.ini

+5-2
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,11 @@ envlist =
66

77
[testenv]
88
description = Unit tests and test coverage
9-
deps = pytest
10-
commands = pytest {posargs}
9+
deps =
10+
django
11+
pytest-django
12+
commands =
13+
pytest {posargs}
1114

1215
[testenv:clean]
1316
description = Remove bytecode and other debris

0 commit comments

Comments
 (0)