Please help With Django Tests in Chapter 4 of Django for Beginners by William Vincent

I am following the tutorial ‘Django for Beginners’ by William Vincent. I am working on chapter 4 where he talks about tests. I have run the tests and out of 8 tests I get one failure. I do not understand why the test is looking for content in the header.
Here is the traceback

Traceback (most recent call last):
  File "C:\Users\ZACKAMATA\Documents\Learn Python\Django\LearnDjango\company\pages\tests.py", l
ine 21, in test_template_content
    self.assertContains(response, "<h1>Homepage of the Zack Amata Company</h1>")
  File "C:\Users\ZACKAMATA\Documents\Learn Python\Django\LearnDjango\company\.venvcompany\Lib\s
ite-packages\django\test\testcases.py", line 623, in assertContains
    self.assertTrue(
AssertionError: False is not true : Couldn't find '<h1>Homepage of the Zack Amata Company</h1>'
 in the following response
b'<header>\n  <a href="/">Home</a>|\n  <a href="/about/">About</a>\n</header>\n\n\n\n<h1>Homepa
ge of the Zack Amata Company </h1>\n<p>The Zack Amata Company Homepage</p>\n<p>The current date
 and time is: Jan. 11, 2025, 6:02 p.m.</p>\n<p> There are 3 items of inventory</p>\n<ul>\n    \
n    <li>Widget 1</li>\n    \n    <li>Widget 2</li>\n    \n    <li>Widget 3</li>\n    \n</ul>\n
<p> Thank You For Visiting.</p>\n\n<p>Click here to check out my <a href="/about">About</a> pag
e</p>\n'

Here is my base.html file

<header>
  <a href="{% url 'home' %}">Home</a>|
  <a href="{% url 'about' %}">About</a>
</header>

{% block content %}{% endblock content %}

The home.html file

{% extends "base.html" %}

{% block content %}

<h1>Homepage of the Zack Amata Company </h1>
<p>The Zack Amata Company Homepage</p>
<p>The current date and time is: {% now "DATETIME_FORMAT" %}</p>
<p> There are {{inventory_list|length }} items of inventory</p>
<ul>
    {% for item in inventory_list  %}
    <li>{{ item }}</li>
    {% endfor %}
</ul>
<p> {{ greeting| title }}</p>

<p>Click here to check out my <a href="/about">About</a> page</p>
{% endblock %}

and the about.html file

{% extends "base.html" %}

{% block content %}
<h1>About Page for the Zack Amata Company</h1>
<p> The address of the Zack Amata Company is {{ contact_address }} and the
    phone number is {{ phone_number}}.
</p>

<p>Click here to check out my <a href="/">Home</a> page</p>
{% endblock content %}

Post the test function that is throwing this error.

from django.test import SimpleTestCase  # type: ignore
from django.urls import reverse  # type: ignore


# Create your tests here.
class HomepageTests(SimpleTestCase):  # type: ignore
    def test_url_exists_at_correct_location(self):
        response = self.client.get("/")
        self.assertEqual(response.status_code, 200)

    def test_url_available_by_name(self):
        response = self.client.get(reverse("home"))
        self.assertEqual(response.status_code, 200)

    def test_template_name_correct(self):
        response = self.client.get(reverse("home"))
        self.assertTemplateUsed(response, "home.html")

    def test_template_content(self):
        response = self.client.get(reverse("home"))
        self.assertContains(response, "<h1>Homepage of the Zack Amata Company</h1>")


class AboutpageTests(SimpleTestCase):  # type: ignore
    def test_url_exists_at_correct_location(self):
        response = self.client.get("/about/")
        self.assertEqual(response.status_code, 200)

    def test_url_available_by_name(self):
        response = self.client.get(reverse("about"))
        self.assertEqual(response.status_code, 200)

    def test_template_name_correct(self):
        response = self.client.get(reverse("about"))
        self.assertTemplateUsed(response, "about.html")

    def test_template_content(self):
        response = self.client.get(reverse("about"))
        self.assertContains(response, "<h1>About Page for the Zack Amata Company</h1>")

The test is looking for this string in the HTML:

<h1>Homepage of the Zack Amata Company</h1>

The first line of your HTML is:

<h1>Homepage of the Zack Amata Company </h1>

Do you see the difference?

1 Like

Wow! This is incredible. Simply awesome. Django is Awesome. @philgyford following your advice I removed the space from the string and the tests returned OK. It worked. Thanks.

Well done.

Given that in most cases white space in HTML doesn’t make any difference to the display, you might want to do the test with the html=True option:

self.assertContains(response, "<h1>Homepage of the Zack Amata Company</h1>", html=True)

This will treat the response contents as HTML, instead of text, which includes ignoring whitespace.

See Testing tools | Django documentation | Django for more info.

And if you ever need to check that a fragment of HTML is equal to a string, instead of containing it, you can use assertHTMLEqual().

2 Likes

That is really good advice @philgyford! I should probably make more use of html=True myself honestly.

Thank you @KenWhitesell for replying earlier, as you so often do on this forum.

And @ZacAmata, I’m pleased you’re going through the book AND you’re engaging with the Django community here. As you can see, it’s one of the best around. People are often willing to help if you prove you’ve made a good attempt, as you did here.

And you experienced one more thing about testing, which I have heard described as “make a test for how the code should work. The test fails. Fix the code. The test passes”.
:+1: