NegassaB
NegassaB

Reputation: 478

AttributeError: 'TestPoll' object has no attribute 'factory', django rest framework, tests

I'm currently working through the django api book to learn django rest framework in a virtualenv. I'm using python=3.6, django=2.2.5 and djangorestframework=3.10.3. I'm on the Testing and Continous Integration chapter which is chapter-8. I'm trying to write the first test on there that is:

from rest_framework.test import APITestCase
from rest_framework.test import APIRequestFactory

from polls import apiviews

# Create your tests here.


class TestPoll(APITestCase):

    def setup(self):
        self.factory = APIRequestFactory()
        self.view = apiviews.PollViewSet.as_view({'get': 'list'})
        self.uri = '/polls/'

    def test_list(self):
        request = self.factory.get(self.uri)
        response = self.view(request)
        self.assertEqual(response.status_code, 200, 'Expected Response Code 200, received {0} instead'.format(response.status_code))

but whenever I run it using python manage.py test it returns with the following error:

Creating test database for alias 'default'...
System check identified no issues (0 silenced).
E
======================================================================
ERROR: test_list (polls.tests.TestPoll)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/home/user/vscodeworkspace/official_django/polls/polls/tests.py", line 17, in test_list
    request = self.factory.get(self.uri)
AttributeError: 'TestPoll' object has no attribute 'factory'

----------------------------------------------------------------------
Ran 1 test in 0.006s

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

I can clearly see the factory attribute and how it's called/accessed. I've tried to access the request and response attributes using self but that has no effect. So what is the problem?

Upvotes: 0

Views: 513

Answers (1)

Daniel Roseman
Daniel Roseman

Reputation: 599630

Your setup method should be called setUp.

Upvotes: 2

Related Questions