0

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 

5 Answers 5

1
def test_abaout_page_status_code(self): response = self.client.get('/about/') self.assertEquals(response.status_code, 200) 

try this

Sign up to request clarification or add additional context in comments.

2 Comments

Then this one is occured: 'AssertionError: 301 != 200'
Oh, it works. I have missed putting this inside the slash. Thank you very much.
1

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) 

1 Comment

Welcome to SO. Please add a few more words to help explain your answer.
0

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) 

Comments

0

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') 

Since it seems like it's hard to find, i'm talking about the first "/" on:('/post/1/').

Thanks for telling me to improve my answers!

2 Comments

You might want to point out, that it is about the leading slash in the second line. Otherwise this is a little hard to read.
Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.
-1

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) 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.