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'
{% 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 %}
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.
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”.