12👍
These other answers are now out of date regarding assertions. The assertion assertHTMLEqual
(since Django 1.4) takes care of things like ignoring whitespace, and ignoring the order of attributes.
For example (from the docs):
from django.test import TestCase
class MyTest(TestCase):
def test_some_html(self):
# A passing test that doesn't raise an AssertionError.
self.assertHTMLEqual(
'<input type="checkbox" checked="checked" id="id_accept_terms" />',
'<input id="id_accept_terms" type="checkbox" checked>'
)
In practice, one of the arguments to assertHTMLEqual
would be dynamically generated.
14👍
I’ve always found a combination of BeautifulSoup, and assertContains and assertFormError from TestCase’s available assertions to do the trick.
- Celery beat not picking up periodic tasks
- How to create custom groups in django from group
- Managing multiple settings.py files
- Django's {{ csrf_token }} is outputting the token value only, without the hidden input markup
- Creating Partial Indexes with Django 1.7
9👍
Django’s test framework is ideal for this.
-
Check the status code and content.
http://docs.djangoproject.com/en/1.2/topics/testing/#django.test.TestCase.assertContains -
Check the template. http://docs.djangoproject.com/en/1.2/topics/testing/#django.test.TestCase.assertTemplateUsed
Also, it helps to use id="something"
tags within your HTML to make it easier to find things when unit testing. We have tests like this.
def should_find_something( self ):
response= self.client.get( "/path/to/resource/pk/" )
self.assertContains( response, '<td id="pk">the pk string</td>', status_code=200 )
self.assertTemplateUsed( response, 'appropriate_page.html' )
Works nicely.
- Creating UTF-8 JsonResponse in Django
- You cannot add messages without installing django.contrib.messages.middleware.MessageMiddleware
- Passing a user, request to forms
- Django-admin.py startproject opens notepad, instead of creating a project
- Is there a command for creating an app using cookiecutter-django?
2👍
Here is the solution with BeautifulSoup:
import bs4 as bs
class HtmlTestMixin:
maxDiff = None
def assertElementContains(self, request_content, html_element="", html_selectors={}, element_text=""):
soup = bs.BeautifulSoup(request_content, "html.parser")
element = soup.find(html_element, **html_selectors)
soup_1 = bs.BeautifulSoup(element_text, "html.parser")
self.assertEqual(str(element.prettify()), soup_1.prettify())
It can be called like:
self.assertElementContains(
response.content,
"select",
{"id": "order-model"},
'''
<select name="order" id="order-model" class="form-select">
<option value="_score" >
Score
</option>
</select>
''',
)
- How to make Django Password Reset Email Beautiful HTML?
- Django database synchronization for an offline usage
1👍
Have a look at Django with asserts – which uses lxml.
- How can i get all models in django 1.8
- How to upgrade Django on ubuntu?
- UnicodeEncodeError:'latin-1' codec can't encode characters in position 0-1: ordinal not in range(256)