When I test the simple app according to tutorial 'AssertionError: 404 != 200' is occured. Can anybody fix this problem? (Project name: simple_project , app name inside the project: pages )
My app-level urls.py:
from django.urls import path
from . import views
urlpatterns = [
path('', views.HomePageView.as_view(), name='home'),
path('about/', views.AboutPageView.as_view(), name='about'),
]
My app-level views.py:
from django.views.generic import TemplateView
class HomePageView(TemplateView):
template_name = 'home.html'
class AboutPageView(TemplateView):
template_name = 'about.html'
My project-level urls.py:
from django.contrib import admin
from django.urls import path, include
urlpatterns = [
path('admin/', admin.site.urls),
path('', include('pages.urls')),
]
My tests.py:
from django.test import SimpleTestCase
class SimpleTests(SimpleTestCase):
def test_home_page_status_code(self):
response = self.client.get('/')
self.assertEquals(response.status_code, 200)
def test_abaout_page_status_code(self):
response = self.client.get('about')
self.assertEquals(response.status_code, 200)
When I test, this error is occured:
FAIL: test_abaout_page_status_code (pages.tests.SimpleTests)
----------------------------------------------------------------------
Traceback (most recent call last):
File "C:\Users\User\Dj\simple\pages\tests.py", line 10, in test_abaout_page_status_code
self.assertEquals(response.status_code, 200)
AssertionError: 404 != 200
def test_abaout_page_status_code(self):
response = self.client.get('/about/')
self.assertEquals(response.status_code, 200)
try this
I had such a problem, use this:
from django.test import TestCase
from django.urls import reverse
def test_abaout_page_status_code(self):
response = self.client.get(reverse("about"))
self.assertEqual(response.status_code, 200)
Well, try this:
from django.urls import reverse, resolve
from django.test import SimpleTestCase
from .views import HomePageView,AboutPageView
class SimpleTests(SimpleTestCase):
def test_home_page_status_code(self):
path = reverse("home")
self.assertEquals(resolve(path).func.view_class, HomePageView)
response = self.client.get(path)
self.assertEquals(response.status_code, 200)
def test_abaout_page_status_code(self):
path = reverse("about")
self.assertEquals(resolve(path).func.view_class, AboutPageView)
response = self.client.get(path)
self.assertEquals(response.status_code, 200)
In case you forgot like I did. remember to add / to your links:
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')
before:
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')
Use mine code bro, this will definitely work!!
from django.test import TestCase
def test_home_page_status_code(self):
response = self.client.get('/')
self.assertEqual(response.status_code, 200)
def test_about_page_status_code(self):
response = self.client.get('/about/')
self.assertEqual(response.status_code, 200)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.