Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/lint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ jobs:
run: |
python -m pip install uv
python -m uv pip install --system -e "."
python -m uv pip install --system -e ".[test]"
python -m uv pip install --system -e ".[lint]"

- name: Lint with flake8
run: |
Expand Down
24 changes: 24 additions & 0 deletions docs/methods.rst
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,30 @@ You might also want to check standard headers::
self.get('my-json-view')
self.assertResponseHeaders({'Content-Type': 'application/json'})

assertResponseTemplateUsed(template\_name, response=None)
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

You can check that a specific template was used to render the last response::

def test_template_used(self):
self.get('my-view')
self.assertResponseTemplateUsed('my_template.html')

This is a convenience wrapper around Django's ``assertTemplateUsed`` that automatically
uses ``self.last_response`` if no response is provided.

assertResponseTemplateNotUsed(template\_name, response=None)
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

You can check that a specific template was not used to render the last response::

def test_template_not_used(self):
self.get('my-view')
self.assertResponseTemplateNotUsed('other_template.html')

This is a convenience wrapper around Django's ``assertTemplateNotUsed`` that automatically
uses ``self.last_response`` if no response is provided.

get\_check\_200(url\_name, \*args, \*\*kwargs)
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ pytest11 = { test_plus = "test_plus.plugin" }

[project.optional-dependencies]
docs = ["sphinx", "furo", "sphinx-copybutton", "sphinx-prompt"]
lint = ["pre-commit"]
lint = ["flake8", "pre-commit"]
test = ["factory-boy", "pytest", "pytest-cov", "pytest-django"]
testing = ["django-test-plus[test]"]

Expand Down
10 changes: 10 additions & 0 deletions test_plus/test.py
Original file line number Diff line number Diff line change
Expand Up @@ -333,6 +333,16 @@ def assertResponseNotContains(self, text, response=None, html=True, **kwargs):
response = self._which_response(response)
self.assertNotContains(response, text, html=html, **kwargs)

def assertResponseTemplateUsed(self, template_name, response=None, **kwargs):
""" Convenience wrapper for assertTemplateUsed """
response = self._which_response(response)
self.assertTemplateUsed(response, template_name, **kwargs)

def assertResponseTemplateNotUsed(self, template_name, response=None, **kwargs):
""" Convenience wrapper for assertTemplateNotUsed """
response = self._which_response(response)
self.assertTemplateNotUsed(response, template_name, **kwargs)

def assertResponseHeaders(self, headers, response=None):
"""
Check that the headers in the response are as expected.
Expand Down
8 changes: 8 additions & 0 deletions test_project/test_app/tests/test_unittests.py
Original file line number Diff line number Diff line change
Expand Up @@ -472,6 +472,14 @@ def test_assertresponsecontains(self):
self.assertResponseContains('<p>Hello world</p>')
self.assertResponseNotContains('<p>Hello Frank</p>')

def test_assertresponsetemplateused(self):
self.get('view-contains')
self.assertResponseTemplateUsed('test.html')

def test_assertresponsetemplatenotused(self):
self.get('view-contains')
self.assertResponseTemplateNotUsed('other.html')

def test_assert_response_headers(self):
self.get('view-headers')
self.assertResponseHeaders({'Content-Type': 'text/plain'})
Expand Down