-1

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_client.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).
F
======================================================================
FAIL: 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 10, in test_homepage
    self.assertEqual(response.status_code, 200)
AssertionError: 404 != 200

----------------------------------------------------------------------
Ran 1 test in 0.003s

FAILED (failures=1)
Destroying test database for alias 'default'...
finegorko
  • 57
  • 8

1 Answers1

0

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) # <--
finegorko
  • 57
  • 8