AssertionError: b 'Working with test classes in Django (TDD)


I'm starting my test studies with Django and am getting an error where it apparently should not, so much so that the description of traceback makes me pretty confused.

I have a class that tests if the return of my view is compatible with its pre-set value, something simple that looks like this:

from django.test import TestCase, Client
from django.core.urlresolvers import reverse

class TestMinhaView(TestCase):

    def setUp(self):
        self.url = reverse('core:spy')
        self.client = Client()

    def test_get(self):
        response = self.client.get(self.url)
        self.assertEqual(response.status_code, 200)
        self.assertEqual(response.content, 'Dados Corretos!')

Made to test the return of this View:

from django.views import generic
from django.http.response import HttpResponse

class MinhaView(generic.View):
    def get(self, request, *args, **kwargs):
        return HttpResponse('Dados Corretos!')

After class modeling, it's time to test! I type in the terminal: test

and then it returns the curious error in your Traceback:

self.assertEqual(response.content, 'Dados Corretos!')
AssertionError: b'Dados Corretos!' != 'Dados Corretos!'

This b makes me curious. What could it be? Because I can not find a logical error since the information in the test and functional class is exactly the same.

Thank you in advance for the patience of those who are willing, and forgiveness if it is a beginner level. Haha.

asked by anonymous 02.06.2017 / 05:02

1 answer


Try to put self.assertEqual(response.content, 'Dados Corretos!.decode("utf-8")') or self.assertEqual(response.content.decode("utf-8"), 'Dados Corretos!') .

This b in front means that the string is encoded in bytes and not in unicode characters, so I suggested using decode .

02.06.2017 / 05:09