mardi 27 janvier 2015

django.test.client got 404 error on existing url

I just started learning unittests and stuck with this problem.


I got project structure like this (it’s Django 1.6.2 now):



./manage.py
./myproject
./myproject/urls.py
./myproject/myapp/
./myproject/myapp/urls.py
./myproject/myapp/views.py
./tests/
./test/test_example.py


In the ./myproject/urls.py I have:



from django.conf.urls import patterns, include, url
urlpatterns = patterns('',
url(r'^myapp/', include('myproject.myapp.urls')),
)


In the ./myproject/myapp/urls.py I have:



from django.conf.urls import patterns, url

urlpatterns = patterns('myproject.myapp.views',
url(r'^example1/$', 'itemlist'),
url(r'^example1/(?P<item_id>\w+)/$', 'item'),
)


I wrote basic test and put it into ./test/test_example.py



import unittest
from django.test import Client

class PagesTestCase(unittest.TestCase):
def setUp(self):
self.client = Client()

def test_itemlist(self):
response = self.client.get('/myapp/example1/')
self.assertEqual(response.status_code, 200)

def test_item(self):
response = self.client.get('/myapp/example1/100100/')
self.assertEqual(response.status_code, 200)


I run this tests from shell like this:



cd ./tests
python manage.py test


First test runs OK, but he second always fails with ‘404 not found’ status code.


Both urls are working OK in the browser.


Also, I tried this:



cd ./
python manage.py shell
>>> from django.test.client import Client
>>> c = Client()
>>> r = c.get('/myapp/example1/100100/')
>>> r.status_code
200


I just can’t figure out how to run those tests properly. It seems no pattern that is passed into views as parameter ever works for me. But all fixed urls are found correctly by django.test.client.


Thank you!


Aucun commentaire:

Enregistrer un commentaire