lundi 26 octobre 2020

Why is this django test is failing?

Whenever I run my test.py I get:

(blog-1rz6wx-6) λ python manage.py test Creating test database for alias 'default'... System check identified no issues (0 silenced). .F.. ====================================================================== FAIL: test_post_detail_view (blog.tests.BlogTests) ---------------------------------------------------------------------- Traceback (most recent call last): File "C:\Users\ **** \Desktop\blog\blog\tests.py", line 40, in test_post_detail_view self.assertEqual(response.status_code, 200) AssertionError: 404 != 200

---------------------------------------------------------------------- Ran 4 tests in 0.776s

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

My test.py code:

def test_post_detail_view(self):
    response = self.client.get('/post/1/')
    no_response = self.client.get('/post/100000')
    self.assertEqual(response.status_code, 200)
    self.assertEqual(no_response.status_code, 404)
    self.assertContains(response, 'A good title')
    self.assertTemplateUsed(response, 'post_detail.html')

urls.py that is in the same folder as settings.py:

from django.contrib import admin
from django.urls import path, include

urlpatterns = [
    path('admin/', admin.site.urls),
    path('',include('blog.urls')),
]

And the urls.py that is located in the same folder as views.py:

from django.urls import path
from .views import BlogListView, BlogDetailView
urlpatterns = [
  path('post/<int:pk>', BlogDetailView.as_view(), name = 'post_detail'),
        path('',BlogListView.as_view(),name = 'home')
    ]

I have no Idea, and when I changed the response to response = self.client.get(''), it does come up with code 200, but the TemplateUsed obviously becomes 'home.html.

Thanks beforehand.

Aucun commentaire:

Enregistrer un commentaire