Why does this test fail?

27 views
Skip to first unread message

Johann Spies

unread,
Dec 4, 2017, 8:19:50 AM12/4/17
to Django users
I am new to Testing Driven Development as well as to Django and will appreciate some help please.

I have just worked through the tutorials of the official documentation for 1.11 and am trying
to get going with TDD and Django.

The app is wos_2017_2 and I get this in the shell (which is run in the project root)

in the app's urls.py I have

>>> from django.test import Client
>>> client = Client()
>>> response = client.get('/')
Not Found: /
>>> response = client.get('/
wos_2017_2/')
>>> response
.status_code
200
from . import views

from django.conf.urls import url

urlpatterns
= [
 url
(r'^$', views.index, name='index'),

]

and the in "wos_2017_2/test.py"

import unittest
from django.test import TestCase
from django.test import Client


# Create your tests here.

from .models import *
from .views import *

class SimpleTest(unittest.TestCase):

   
def test_index(self):
        client
= Client()
        response
= client.get('/')
       
self.assertEqual(response.status_code, 200)

And the test fails with
FAIL: test_index (wos_2017_2.tests.SimpleTest)
----------------------------------------------------------------------
Traceback (most recent call last):
 
File "/home/js/django/wos/wos_2017_2/tests.py", line 16, in test_index
   
self.assertEqual(response.status_code, 200)
AssertionError: 404 != 200

----------------------------------------------------------------------
Ran 1 test in 0.006s

Using the url
http://localhost:8000/wos_2017_2/

in a browser works as expected.

Johann

Johann Spies

unread,
Dec 4, 2017, 9:16:42 AM12/4/17
to Django users
My question was answered on Stackoverflow:

The correct line in the function should be


response = client.get('/wos_2017_2/')

because of my project urls.py that I did not provide initially:

urlpatterns = [
 url
(r'^wos_2017_2/', include('wos_2017_2.urls')),
 url
(r'^admin/', admin.site.urls),
]



 
Reply all
Reply to author
Forward
0 new messages