Re: Unit testing - self.assertEqual("

Break it!

", resp.content)
Hi,
I have been using assertEqual lately in my unit testing to deliberately make coverage break.  By comparing <h2>Something False</h2> with the complete HTML returned (resp.content).

    self.assertEqual("<h2>Something False</h2>", resp.content)

In order to reveal and debug these 3 links.

    self.assertNotContains(resp, '<a href="/admin/doc/">Documentation</a>')
    self.assertNotContains(resp, '<a href="/admin/password_change/">Change password</a>')
    self.assertNotContains(resp, '<a href="/admin/logout/">Log out</a>')


But the unit test debug output of the complete HTML returned doesn't reveal those 3 links.  Why is that?

unit test output
http://dpaste.com/23XGR4M.txt

Compared to the HTML returned that I can see by manually checking the source page from my browser.

html source page
http://dpaste.com/0PCZJ0D

<div id="user-tools"> Welcome, <strong>joe</strong>.
<a href="/admin/doc/">Documentation</a> /
<a href="/admin/password_change/">Change password</a> /
<a href="/admin/logout/">Log out</a>

</div>


Is there something wrong with my overridden template file?  Where I have removed {% block userlinks %} in order to reveal Django admins above 3 default links.

password_reset_form.html
http://dpaste.com/0T3N3JK

{% extends "admin/base_site.html" %}
{% load i18n %}

{% comment %}
{% block userlinks %}
{% trans "Change password" %} / <a href="/accounts/logout/">{% trans "Log out" %}</a>
{% endblock %}
{% endcomment %}



{% comment %}
{% block breadcrumbs %}
<div class="breadcrumbs">
<a href="{% url 'navi_polls:index' %}">{% trans "Home" %}</a>
&rsaquo; {% trans "Password reset" %}
</div>
{% endblock %}
{% endcomment %}

{% block title %}{% trans "Password reset - v.2" %}{% endblock %}


{% block content %}
<h1>{% trans "Password reset" %}</h1>
<p>{% trans "Forgotten your password? Enter your email address below, and we'll email instructions for setting a new one." %}</p>

<form action="" method="post">{% csrf_token %}
{{ form.email.errors }}
<p><label for="id_email">{% trans "Email address:" %}</label> {{ form.email }} <input type="submit" value="{% trans 'Reset my password' %}" /></p>
</form>
{% endblock %}












On Monday, July 7, 2014 6:08:20 AM UTC+2, mitesh_django wrote:

Hi,

You are trying to compare <h2> break it!<h2>  with the complete HTML returned. Instead of assertEqual, use assertIn.

self.assertIn("<h2>Break it!</h2>", resp.content)

Regards,
Mitesh

On 7 Jul 2014 01:44, "Pepsodent Cola" <pepsod...@gmail.com> wrote:
When I run this unit test to make it break so it can output a bunch of html data.

    self.assertEqual("<h2>Break it!</h2>", resp.content)


test.py
# Reset password no login

def test_Reset_no_login(self):
    self.assertTrue(isinstance(self.user, User))
    login = self.client.login(username='captain', password='america')

    self.assertEqual("<h2>Profile</h2>", resp.content)

    self.assertNotContains(resp, '<a href="/admin/doc/">Documentation</a>')
    self.assertNotContains(resp, '<a href="/admin/password_change/">Change password</a>')
    self.assertNotContains(resp, '<a href="/admin/logout/">Log out</a>')



Then I can't find these html tags in unit test's resp.content but when I manually check the html source page from the web browser then I see those 3 html tags.
Why isn't unit test resp.content output displaying these 3 html lines?


html source page
http://dpaste.com/0PCZJ0D

<div id="user-tools"> Welcome, <strong>joe</strong>.
<a href="/admin/doc/">Documentation</a> /
<a href="/admin/password_change/">Change password</a> /
<a href="/admin/logout/">Log out</a>

</div>


unit test output
http://dpaste.com/23XGR4M.txt

======================================================================
FAIL: test_Reset_no_login (userprofile.tests.PasswordResetTest)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/home/Python/Projects/Navi/Django/navi/userprofile/tests.py", line 132, in test_Reset_no_login

    self.assertEqual("<h2>Profile</h2>", resp.content)

AssertionError: '<h2>Profile</h2>' != '<!DOCTYPE html>\n<html lang="en-us" >\n<head>\n<title>Password reset - v.2</title>\n<link rel="stylesheet" type="text/css" href="/static/admin/css/base.css" />\n\n<!--[if lte IE 7]><link rel="stylesheet" type="text/css" href="/static/admin/css/ie.css" /><![endif]-->\n\n<script type="text/javascript">window.__admin_media_prefix__ = "/static/admin/";</script>\n\n<meta name="robots" content="NONE,NOARCHIVE" />\n</head>\n\n\n<body class="">\n\n<!-- Container -->\n<div id="container">\n\n    \n    <!-- Header -->\n    <div id="header">\n        <div id="branding">\n        \n<h1 id="site-name">Navi administration - v.1</h1>\n\n        </div>\n        \n        <p>Admin navigation</p>\n    </div>\n    <!-- END Header -->\n    \n    <div class="breadcrumbs">\n    <a href="/admin/">Home</a>\n    \n    </div>\n    \n    \n\n    \n        \n    \n\n    <!-- Content -->\n    <div id="content" class="colM">\n        \n        \n        \n<h1>Password reset</h1>\n<p>Forgotten your password? Enter your email address below, and we\'ll email instructions for setting a new one.</p>\n\n<form action="" method="post"><input type=\'hidden\' name=\'csrfmiddlewaretoken\' value=\'iXJku3wGmCRg95SlHqC5QfHEH48XCRRZ\' />\n\n<p><label for="id_email">Email address:</label> <input id="id_email" maxlength="254" name="email" type="text" /> <input type="submit" value="Reset my password" /></p>\n</form>\n\n        \n        <br class="clear" />\n    </div>\n    <!-- END Content -->\n\n    <div id="footer"></div>\n</div>\n<!-- END Container -->\n\n</body>\n</html>\n'

----------------------------------------------------------------------
Ran 36 tests in 5.981s

FAILED (failures=1)
Destroying test database for alias 'default' (':memory:')...


--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/c20a58c5-fad3-4ba6-906e-ca99158f9db6%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/d8df1c2a-f1a5-48c3-aeeb-0ec37378931c%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.