Django test client returns 404, but works in the shell
This code works fine in the shell, but if you run it through python manage.py test it throws a 404 error, what could be the problem?
test_urls.py
from django.test import Client, TestCase
class StaticURLTests(TestCase):
def setUp(self):
self.guest_client = Client()
def test_homepage(self):
response = self.guest_clientw.get("/")
self.assertEqual(response.status_code, 200)
error:
$ python manage.py test
Creating test database for alias 'default'...
System check identified no issues (0 silenced).
E
======================================================================
ERROR: test_homepage (posts.tests.test_urls.StaticURLTests)
----------------------------------------------------------------------
Traceback (most recent call last):
File "/Users/finegorko/Development/Yandex.Practicum/hw03_forms/yatube/posts/tests/test_urls.py", line 9, in test_homepage
response = self.guest_client.GET.get("/")
AttributeError: 'Client' object has no attribute 'GET'
----------------------------------------------------------------------
Ran 1 test in 0.000s
FAILED (errors=1)
Destroying test database for alias 'default'...
The problem was that a test database was being created that did not have a group.
In views.py an object with the function get_object_or_404() was passed to the index context.
def index(request):
...
group = get_object_or_404(Group) # <--