Saturday, March 30, 2019

Re: Unit-Testing Django Views

The online version of the book is pinned to Django 1.11. You may want to check that out.

-Jorge

On Sat, Mar 30, 2019 at 4:40 AM Test Bot <onlinejudge95@gmail.com> wrote:
Sorry for the bother but I finally solved it by using Django's Test Client and checking for status_code and template used to render the response in my unit-test.

Regards,
Test Bot

On Sat, Mar 30, 2019 at 5:00 PM Test Bot <onlinejudge95@gmail.com> wrote:
I tried removing the {% csrf_token %} from my index.html But it seemed to have no effect. I got the following traceback for further clarity

                                                                                                              START OF TRACEBACK
===================================================================================================================================
python superlists/manage.py test
Creating test database for alias 'default'...
System check identified no issues (0 silenced).
.F.
Destroying test database for alias 'default'...
======================================================================
FAIL: test_index_view_returns_correct_html (lists.tests.HomePageTest)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "superlists/lists/tests.py", line 25, in test_index_view_returns_correct_html
    self.assertEqual(expected_html, actual_html)
AssertionError: '<!DO[267 chars]lue="UUl2QtIopzqg9Co4uPlTlSuEPP2O44aMhda056gd4[201 chars]l>\n' != '<!DO[267 chars]lue="46hpOIUSFV0Qm4Z4cwq9OORHGeLPcNlGrp6n3lsHk[201 chars]l>\n'

----------------------------------------------------------------------
Ran 3 tests in 0.005s

FAILED (failures=1)

Process finished with exit code 1
===================================================================================================================================
                                                                                                                   END OF TRACEBACK


On Fri, Mar 29, 2019 at 12:58 AM Aldian Fazrihady <mobile@aldian.net> wrote:
There are several things you can try:
1. Mocking csrf token functions
2. Passing the csrf token context from first HTML generation to the second HTML generation
3. Wiping out the csrf token parts from both HTML before comparing them.

On Thu, 28 Mar 2019, 23:54 Simon Charette, <charette.s@gmail.com> wrote:
This is effectively failing because of a mechanism added in 1.10 to protect
against BREACH attacks[0] by salting the CSRF token.

I'm not aware of any way to disable this mechanism but testing against the
exact HTML returned from a view seems fragile.

I suggest you use assertContains[1] (with or without html=True) and
assertTemplateUsed[2] instead.

Cheers,
Simon


Le jeudi 28 mars 2019 12:16:43 UTC-4, OnlineJudge95 a écrit :
Hi people,

I am following the book Test-Driven Development with Python by Harry J.W. Perceval. The book is using Django version 1.7 which is outdated as of now so I started with version 2.1.

I am trying to unit test my index view. One unit-test that I have written is testing whether the index view returns correct HTML or not by comparing the input received through
django.template.loader.render_to_string
the unit-test fail with the following traceback
python manage.py test
Creating test database for alias 'default'...
.System check identified no issues (0 silenced).
F.
======================================================================
FAIL: test_index_view_returns_correct_html (lists.tests.IndexViewTest)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "tests.py", line 24, in test_index_view_returns_correct_html
    self.assertEqual(expected_html, actual_html)
AssertionError: '<!DO[263 chars]lue="BJMT1b9fxuXOGugp00SDypeTYZxvlmc6KtBSYMDon[198 chars]l>\n' != '<!DO[263 chars]lue="R05ZiWMASEWMurA8Rdo8bnA0mTwqFTqA0KUYfxgJI[198 chars]l>\n'

----------------------------------------------------------------------
Ran 3 tests in 0.006s

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

Process finished with exit code 1


It was clear that the csrf token is causing the test to fail. Is there any way to test it, or should it be tested? I ask this as when I changed my Django version to 1.7, the tests were passing, even after giving the csrf token field in the form. I tried going through the changelogs but 1.7 is far behind (beginner here). Please find the code snippets, directory structure provided below.


lists/views.py
from django.http import HttpResponse
from django.shortcuts import render


# Create your views here.
def index(request):
    if request.method == 'POST':
        return HttpResponse(request.POST['item_text'])
    return render(request, 'index.html')

lists/test.py
from django.http import HttpRequest
from django.template.loader import render_to_string
from django.test import TestCase
from django.urls import resolve

from lists.views import index


# Create your tests here.
class IndexViewTest(TestCase):

    def test_root_url_resolves_to_home_page_view(self):
       [...]

    def test_index_view_returns_correct_html(self):
        request = HttpRequest()
        response = index(request)
        actual_html = response.content.decode()
        expected_html = render_to_string('index.html', request=request)

        self.assertEqual(expected_html, actual_html)

    def test_index_view_can_save_a_post_request(self):
       [...]

requirements.txt
Django==2.1.7
pytz==2018.9
selenium==3.141.0
urllib3==1.24.

settings.py
[...]
# Application definition

INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'lists',
]

[...]

Directory Structure

Screen Shot 2019-03-28 at 9.36.56 PM.png


--
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 https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/7f0f12be-fc3e-43c3-ba07-e48158def051%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 https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CAN7EoAb8Ov7GXZXV7Eao%3Dj_-w4bKKG75ebF0fnjbTzHO%2BxmB%3DA%40mail.gmail.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 https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CAD%3DM5eRFp%3DV6RT7ZTpVroxXyM5tosv9fJ-WQnRQm82AgOcNQ0Q%40mail.gmail.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 https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CANfN%3DK9qJr48TSmazAXjQdEY83Ec%2B%2BOWDcWixzP7vUqCYraXWA%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment