diff --git a/.github/workflows/lint.yml b/.github/workflows/lint.yml index efb3cb0..8323cec 100644 --- a/.github/workflows/lint.yml +++ b/.github/workflows/lint.yml @@ -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: | diff --git a/docs/methods.rst b/docs/methods.rst index 454622d..e626682 100644 --- a/docs/methods.rst +++ b/docs/methods.rst @@ -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) ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ diff --git a/pyproject.toml b/pyproject.toml index 9dbd273..afcaba5 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -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]"] diff --git a/test_plus/test.py b/test_plus/test.py index 723dfe1..247e451 100644 --- a/test_plus/test.py +++ b/test_plus/test.py @@ -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. diff --git a/test_project/test_app/tests/test_unittests.py b/test_project/test_app/tests/test_unittests.py index 5e41343..880c04e 100644 --- a/test_project/test_app/tests/test_unittests.py +++ b/test_project/test_app/tests/test_unittests.py @@ -472,6 +472,14 @@ def test_assertresponsecontains(self): self.assertResponseContains('

Hello world

') self.assertResponseNotContains('

Hello Frank

') + 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'})