Chapter 5 can't pass decode...

1,610 views
Skip to first unread message

martin wang

unread,
Dec 9, 2015, 2:16:21 AM12/9/15
to Obey the testing goat! Test-Driven Web Development with Python book
I have got fail in unit test
 
FAIL: test_home_page_can_save_request_POST (lists.tests.HomePageTest)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "D:\superlists\lists\tests.py", line 32, in test_home_page_can_save_reque
st_POST
    self.assertEqual(response.content.decode(),excepted_html)
AssertionError: '<htm[206 chars]t\t\t<input type=\'hidden\' name=\'csrfmiddlew[1
79 chars]tml>' != '<htm[206 chars]t\t\t\n\t\t</form>\n\t\t\n\t\t<table id="id_li
[82 chars]tml>'

======================================================================
FAIL: test_home_page_return_correct_html (lists.tests.HomePageTest)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "D:\superlists\lists\tests.py", line 19, in test_home_page_return_correct
_html
    self.assertEqual(response.content.decode(),excepted_html)
AssertionError: '<htm[206 chars]t\t\t<input type=\'hidden\' name=\'csrfmiddlew[1
64 chars]tml>' != '<htm[206 chars]t\t\t\n\t\t</form>\n\t\t\n\t\t<table id="id_li
[67 chars]tml>'

----------------------------------------------------------------------
Ran 4 tests in 0.016s

FAILED (failures=2, errors=1)
but if I remove {% csrf_token %}, it will be ok


D:\superlists>python manage.py test
Creating test database for alias 'default'...
...
----------------------------------------------------------------------
Ran 3 tests in 0.016s

OK
Destroying test database for alias 'default'...


But the function test get the CSRF problem...

so what should I do to pass through the fail from unit test decode problem?



Harry Percival

unread,
Dec 10, 2015, 2:40:26 AM12/10/15
to martin wang, Obey the testing goat! Test-Driven Web Development with Python book
Hi Martin,

Can you show me the code you have for that test?  It should look like this:

    def test_home_page_returns_correct_html(self):
        request = HttpRequest()
        response = home_page(request)
        expected_html = render_to_string('home.html')
        self.assertEqual(response.content.decode(), expected_html)

Which version of django are you using?

--
You received this message because you are subscribed to the Google Groups "Obey the testing goat! Test-Driven Web Development with Python book" group.
To unsubscribe from this group and stop receiving emails from it, send an email to obey-the-testing-go...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.



--
------------------------------
Harry J.W. Percival
------------------------------
Twitter: @hjwp
Mobile:  +44 (0) 78877 02511
Skype:         harry.percival

Harry Percival

unread,
Dec 10, 2015, 10:04:46 AM12/10/15
to martin wang, Obey the testing goat! Test-Driven Web Development with Python book
Hi everyone,

I can confirm this is a problem introduced by django 1.9.  Stick to django 1.8 for this book.  It's an LTS ("Long Term Support") edition.  1.8.7 is the latest, and should work fine....

HP

martin wang

unread,
Dec 10, 2015, 7:23:09 PM12/10/15
to Obey the testing goat! Test-Driven Web Development with Python book, martinw...@gmail.com, hj...@cantab.net

HI Harry:

Thanks for reply...
It's okay now, I don't know why but the assertTrue can pass this problem.
I think I will change my version back to 1.8

Harry Percival

unread,
Jan 4, 2016, 7:14:23 PM1/4/16
to martin wang, Obey the testing goat! Test-Driven Web Development with Python book
Hi there, for anyone else that's curious about how to do this in 1.9 @marcoslhc suggested a good fix:  you can pass a request object to rendet_to_response, and it will add a csrf token for you:

https://github.com/hjwp/book-example/issues/8

I still recommend ppl stick to django 1.8 for now.  You can always learn django 1.9 later, and you don't want to waste too much time figuring out, when things look different on your pc from what i see in the book, whether it's a mistake in your code or just a django difference...

--
You received this message because you are subscribed to the Google Groups "Obey the testing goat! Test-Driven Web Development with Python book" group.
To unsubscribe from this group and stop receiving emails from it, send an email to obey-the-testing-go...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Steve Groesz

unread,
Jun 5, 2016, 12:36:16 AM6/5/16
to Obey the testing goat! Test-Driven Web Development with Python book
Ok, I ran into this as well.  I did some research, and couldn't find much information about render_to_string, especially not in the context used in ch 5.  Before this, I had quietly wondered how the test would handle the CSRF token, given that it should be different between instances of the rendered response.

What I found online were references to render_to_response, which was introduced back in django 1.3.  render came later, and per the docs, render_to_response may be deprecated in the future.  Render is part of the django.shortcuts package.

Some things to know about render:
- It returns an HttpResponse object, whose content is a byte string
- it requires a request object as well as the template to render
- if the request object is different from that used to generate the response we are testing against, the CSRF token will be different between the two responses

As far as I can tell, render seems to be the preferred way to accomplish the test we are trying to write.  Here's the changes I made to my code to get it to work:

from django.template.loader import render_to_string
+ from django.shortcuts import render

def test_home_page_returns_correct_html(self):
[...]
-   expected_html = render_to_string('home.html')
-   self.assertEqual(response.content.decode(), expected_html)
+ expected_response = render(request, 'home.html')
+   self.assertEqual(response.content.decode(), expected_response.content.decode())
[...]

def test_home_page_can_save_a_POST_request(self):
[...]
-    expected_html = render_to_string(
-        'home.html',
-        {'new_item_text': 'A new list item'}
-    )
-    self.assertEqual(response.content.decode(), expected_html)
+   expected_response = render(
+       request,
+       'home.html',
+       {'new_item_text': 'A new list item'}
+   )
+   self.assertEqual(response.content.decode(), expected_response.content.decode())
[...]

I tested these changes with Django versions 1.8.4, 1.9.1 and 1.9.7, and all pass.

For giggles, I tried creating a request2 = HttpRequest() and using request in the home_page function and request2 in the render function, and of course the tests failed because the CSRF tokens were different.

I wonder if
self.assertEqual(response, expected_response)
would pass...

I did learn a lot testing and tracking down this problem.  So, not a complete waste.  Hopefully this will help someone else who [,like me, can't follow directions] runs into this same problem :)

Anyway, now to get back on with chapter 5 :P

Todd Meyers

unread,
Jun 6, 2016, 1:32:24 PM6/6/16
to Obey the testing goat! Test-Driven Web Development with Python book
Hi Steve,

I ran into the same problem and your changes worked perfectly for me.  Thanks for your very helpful explanation and corrections.

Todd

Daniel Brownridge

unread,
Jun 19, 2016, 6:47:04 AM6/19/16
to Obey the testing goat! Test-Driven Web Development with Python book
I think comparing the raw responses is too far, but it is also probably unnecessary to use decode() on both content objects.
Hence the middle ground of comparing the two content bytestrings of the response objects directly seems reasonable.
self.assertEqual(response.content, expected_response.content)
I think this is OK because an error in a byte string will still be readable in an error message.
Somebody please feel free to prove me wrong though ...


On Sunday, June 5, 2016 at 5:36:16 AM UTC+1, Steve Groesz wrote:
Ok, I ran into this as well.  I did some research, and couldn't find much information about render_to_string, especially not in the context used in ch 5.  Before this, I had quietly wondered how the test would handle the CSRF token, given that it should be different between instances of the rendered response.

What I found online were references to render_to_response, which was introduced back in django 1.3.  render came later, and per the docs, render_to_response may be deprecated in the future.  Render is part of the django.shortcuts package.

Some things to know about render:
- It returns an HttpResponse object, whose content is a byte string
- it requires a request object as well as the template to render
- if the request object is different from that used to generate the response we are testing against, the CSRF token will be different between the two responses

As far as I can tell, render seems to be the preferred way to accomplish the test we are trying to write.  Here's the changes I made to my code to get it to work:

from django.template.loader import render_to_string
+ from django.shortcuts import render

def test_home_page_returns_correct_html(self):
[...]
-   expected_html = render_to_string('home.html')
-   self.assertEqual(response.content.decode(), expected_html)
+ expected_response = render(request, 'home.html')

Dieter Van Eessen

unread,
Jun 26, 2016, 12:50:57 PM6/26/16
to Obey the testing goat! Test-Driven Web Development with Python book
Hello,

Found the answer on stackoverflow:
http://stackoverflow.com/questions/34629261/django-render-to-string-ignores-csrf-token

Simply adding 'request=request' as input to the render_to_string() function resolves the problem.

kind regards,
Dieter

Harry Percival

unread,
Jun 27, 2016, 2:18:46 PM6/27/16
to Dieter Van Eessen, Obey the testing goat! Test-Driven Web Development with Python book
Hi Dieter, the problem is that you're using the wrong version of django.  switch back to django 1.8 and there's no need for the request argument.

The rest of the book assumes you're using django 1.8, so if you try and stick with using 1.9, you'll likely run into differences that will be hard to debug.  you can always use 1.9 when it comes to your own projects.

--
You received this message because you are subscribed to the Google Groups "Obey the testing goat! Test-Driven Web Development with Python book" group.
To unsubscribe from this group and stop receiving emails from it, send an email to obey-the-testing-go...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
--

--
Harry Percival
+44 78877 02511

vinz

unread,
Mar 18, 2017, 11:27:22 AM3/18/17
to Obey the testing goat! Test-Driven Web Development with Python book, dieter.v...@gmail.com
Ok I understand that the book is written for django 1.8 But I want to learn TDD for my own use with the last version of django. So I had to adapt the example.
So this discussion is very interesting for my point .
Also if you know about things will be incompatible with futur version of django and you can make it compatible for 1.8 and these futur version of django, your book will just be better


Harry Percival

unread,
Mar 18, 2017, 3:11:15 PM3/18/17
to vinz, Obey the testing goat! Test-Driven Web Development with Python book, dieter.v...@gmail.com

Glad you got something out of it anyway vinz! For info the book is now upgraded to python 3.6 and django 1.11beta.  The online version is live, pdf should follow in a couple of weeks.

Hp


--
You received this message because you are subscribed to the Google Groups "Obey the testing goat! Test-Driven Web Development with Python book" group.
To unsubscribe from this group and stop receiving emails from it, send an email to obey-the-testing-go...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

vinz

unread,
Mar 18, 2017, 3:49:09 PM3/18/17
to Obey the testing goat! Test-Driven Web Development with Python book, vinc...@gmail.com, dieter.v...@gmail.com
Yes it was a good exercice to search for a solution

The upgrade for 1.11 is a very good news
Thank you for your work, it is a great course

Jezrael Arciaga

unread,
Jun 17, 2017, 11:55:28 PM6/17/17
to Obey the testing goat! Test-Driven Web Development with Python book
i've run into similar problem, but rather than changing the code i just introduced additional steps which removes csrf_token

def remove_csrf_tag(text):
    """Remove csrf tag from TEXT"""
    return re.sub(r'<[^>]*csrfmiddlewaretoken[^>]*>', '', text)

...
# then change assertion to
self.assertEqual(
        remove_csrf_tag(response.content),
        remove_csrf_tag(expected_html)
    )


Harry Percival

unread,
Jun 18, 2017, 10:07:00 AM6/18/17
to Jezrael Arciaga, Obey the testing goat! Test-Driven Web Development with Python book

Y'all have me worried now. I thought this problem was comprehensively fixed in the new edition. Are you still using the old version of the book?


--
You received this message because you are subscribed to the Google Groups "Obey the testing goat! Test-Driven Web Development with Python book" group.
To unsubscribe from this group and stop receiving emails from it, send an email to obey-the-testing-go...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Asher Diamant

unread,
Jul 23, 2017, 1:49:26 PM7/23/17
to Obey the testing goat! Test-Driven Web Development with Python book, jezar...@gmail.com
Running into the same issue I found this topic. I just downloaded the updated book from my O'Reilly account and it still references Django 1.8, not 1.11. 
I have not signed up for Safari, so can't check the online version, but the download version is definitely not updated yet.
To unsubscribe from this group and stop receiving emails from it, send an email to obey-the-testing-goat-book+unsub...@googlegroups.com.

For more options, visit https://groups.google.com/d/optout.

Harry Percival

unread,
Jul 23, 2017, 4:04:17 PM7/23/17
to Asher Diamant, Obey the testing goat! Test-Driven Web Development with Python book, jezar...@gmail.com
unfortunately oreilly aren't selling pdfs any more.  but the free version here is bang up-to-date: http://www.obeythetestinggoat.com/pages/book.html

To unsubscribe from this group and stop receiving emails from it, send an email to obey-the-testing-go...@googlegroups.com.

For more options, visit https://groups.google.com/d/optout.
--

--
Harry Percival
+44 78877 02511

--
You received this message because you are subscribed to the Google Groups "Obey the testing goat! Test-Driven Web Development with Python book" group.
To unsubscribe from this group and stop receiving emails from it, send an email to obey-the-testing-go...@googlegroups.com.

For more options, visit https://groups.google.com/d/optout.

Asher Diamant

unread,
Jul 23, 2017, 4:51:52 PM7/23/17
to Obey the testing goat! Test-Driven Web Development with Python book, asher....@gmail.com, jezar...@gmail.com
Right, but they're still supposedly keeping their books up to date. From their announcement email:
  • We'll alert you when those products are updated, and you can download the revised version from the "Your Products" page.
So I was assuming that if the author is sending them updates, they'll keep updating the books - but it might work completely differently in practice, you'll know better than me :)
Thanks for the link.
To unsubscribe from this group and stop receiving emails from it, send an email to obey-the-testing-goat-book+unsub...@googlegroups.com.

For more options, visit https://groups.google.com/d/optout.
--

--
Harry Percival
+44 78877 02511

--
You received this message because you are subscribed to the Google Groups "Obey the testing goat! Test-Driven Web Development with Python book" group.
To unsubscribe from this group and stop receiving emails from it, send an email to obey-the-testing-goat-book+unsub...@googlegroups.com.

For more options, visit https://groups.google.com/d/optout.

Bill McMillin

unread,
Aug 24, 2017, 1:43:03 PM8/24/17
to Obey the testing goat! Test-Driven Web Development with Python book
I had the same issue. Bought the ebook long ago and can still download it from my O'Reilly account. It references Django 1.8. Since they transitioned to Safari only O'Reilly has continued to send me updates for other ebooks I had purchased previously, so they might not have the updated files for this title posted yet. 

The link to the updated version is very helpful. Thanks for posting.

Jeffrey Allman

unread,
Aug 25, 2017, 8:21:15 PM8/25/17
to Obey the testing goat! Test-Driven Web Development with Python book
Publishers consider new editions of a book to be a whole new book, new ISBN and everything, rather than an update. Kinda sucks, but I think that's what you're running into.

I love it that Harry negotiated to make it available free online for everyone!

Harry Percival

unread,
Aug 26, 2017, 4:37:40 AM8/26/17
to Jeffrey Allman, Obey the testing goat! Test-Driven Web Development with Python book

They are good, O'Reilly.  I'm sad they stopped selling pdfs directly, because amazon are all drm-ey and google's pdfs are completely broken.  But I found out that ebooks.com has nice drm-free editions, so the book is available to buy in good formats to buy. I've linked to them on obeythetestinggoat.com.

And the print edition is due out in 2 days time!

Help yourselves to the free edition in the meantime :)

HP


On Sat, 26 Aug 2017, 01:21 Jeffrey Allman <jallm...@gmail.com> wrote:
Publishers consider new editions of a book to be a whole new book, new ISBN and everything, rather than an update. Kinda sucks, but I think that's what you're running into.

I love it that Harry negotiated to make it available free online for everyone!

--
You received this message because you are subscribed to the Google Groups "Obey the testing goat!  Test-Driven Web Development with Python book" group.
To unsubscribe from this group and stop receiving emails from it, send an email to obey-the-testing-go...@googlegroups.com.

For more options, visit https://groups.google.com/d/optout.

Jeffrey Allman

unread,
Aug 26, 2017, 8:56:11 AM8/26/17
to Obey the testing goat! Test-Driven Web Development with Python book, jallm...@gmail.com
Oh yes! I didn't mean to impugn O'Reilly.  I've never read a bad O'Reilly book and I have a Safari account I use nearly every day.  And they do so much more than that for the tech world.

I just meant it feels kinda sucky when you buy the first edition of a book because the second edition is coming out soon and you expect to get it as an update.  I think I did that years ago for a different book before getting Safari.

Harry, I bought the first edition of your book years ago and love it, never got all the way through it.  I just started the second edition and I'm really looking forward to the new stuff!

On Saturday, August 26, 2017 at 4:37:40 AM UTC-4, Harry Percival wrote:

They are good, O'Reilly.  I'm sad they stopped selling pdfs directly, because amazon are all drm-ey and google's pdfs are completely broken.  But I found out that ebooks.com has nice drm-free editions, so the book is available to buy in good formats to buy. I've linked to them on obeythetestinggoat.com.

And the print edition is due out in 2 days time!

Help yourselves to the free edition in the meantime :)

HP


On Sat, 26 Aug 2017, 01:21 Jeffrey Allman <jallm...@gmail.com> wrote:
Publishers consider new editions of a book to be a whole new book, new ISBN and everything, rather than an update. Kinda sucks, but I think that's what you're running into.

I love it that Harry negotiated to make it available free online for everyone!

--
You received this message because you are subscribed to the Google Groups "Obey the testing goat!  Test-Driven Web Development with Python book" group.
To unsubscribe from this group and stop receiving emails from it, send an email to obey-the-testing-goat-book+unsub...@googlegroups.com.

For more options, visit https://groups.google.com/d/optout.

Harry Percival

unread,
Aug 26, 2017, 10:23:05 AM8/26/17
to Jeffrey Allman, Obey the testing goat! Test-Driven Web Development with Python book
:-D

Thanks for the kind words Jeffrey. Let me know how you like it the second time round!



To unsubscribe from this group and stop receiving emails from it, send an email to obey-the-testing-go...@googlegroups.com.

For more options, visit https://groups.google.com/d/optout.
--

--
Harry Percival
+44 78877 02511

--
You received this message because you are subscribed to the Google Groups "Obey the testing goat! Test-Driven Web Development with Python book" group.
To unsubscribe from this group and stop receiving emails from it, send an email to obey-the-testing-go...@googlegroups.com.

For more options, visit https://groups.google.com/d/optout.
Message has been deleted

Nok Lam Chan

unread,
Aug 7, 2018, 2:39:54 AM8/7/18
to Obey the testing goat! Test-Driven Web Development with Python book
This method does not work on Django 2.1, csrf token changes every time even if I pass the same object. Also, test request=request which is essentially the same thing, does not work.... any solution around?

Harry Percival

unread,
Aug 7, 2018, 5:23:05 AM8/7/18
to Nok Lam Chan, Obey the testing goat! Test-Driven Web Development with Python book
please don't use Django 2 to follow along with the book.  http://www.obeythetestinggoat.com/book/pre-requisite-installations.html#_django

--
You received this message because you are subscribed to the Google Groups "Obey the testing goat! Test-Driven Web Development with Python book" group.
To unsubscribe from this group and stop receiving emails from it, send an email to obey-the-testing-go...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Reply all
Reply to author
Forward
0 new messages