writing unit tests for views?

522 views
Skip to first unread message

bshaurette

unread,
Jul 14, 2008, 8:28:31 PM7/14/08
to Django users
Writing tests for models has been a snap, but I'm having a lot harder
time with the views.

I'm trying to use the test client, something like this:


from django.test import Client, TestCase

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

def test_view_profile(self):
response = self.client.get('/accounts/profile/', {'username':
'newuser1'})

# Check some response details
self.assertEqual(response.status_code, 200)
self.assertContains(response, 'Profile View')


But I'm getting errors related to the project settings, most
specifically this one:

File "/Users/bshaurette/Code/django_trunk/django/core/handlers/
base.py", line 126, in get_response
subject = 'Error (%s IP): %s' % ((request.META.get('REMOTE_ADDR')
in settings.INTERNAL_IPS and 'internal' or 'EXTERNAL'), request.path)
TypeError: 'in <string>' requires string as left operand

I tried setting an INTERNAL_IPS value to get around that, but there
are other settings errors that pop up. Ultimately, it looks like the
test is having trouble reconciling paths between the project urls.py
and the app urls.py ... ?


DoesNotExist: Site matching query does not exist.


I'm not new to writing tests, just to unit testing in Django/Python -
what would *really* help is if I could see some other examples of view
testing. I feel like I've been banging my head against a wall for a
few days now. Has anyone got any recommendations?

Russell Keith-Magee

unread,
Jul 14, 2008, 10:17:11 PM7/14/08
to django...@googlegroups.com
On Tue, Jul 15, 2008 at 8:28 AM, bshaurette <bshau...@gmail.com> wrote:
>
> Writing tests for models has been a snap, but I'm having a lot harder
> time with the views.
...

> I'm not new to writing tests, just to unit testing in Django/Python -
> what would *really* help is if I could see some other examples of view
> testing. I feel like I've been banging my head against a wall for a
> few days now. Has anyone got any recommendations?

The Django system tests contain a lot of examples of Django view
tests. In particular, modeltests/test_client and
regressiontests/test_client_regress show how to exercise most of the
features of the Django test system.

Yours
Russ Magee %-)

pgb

unread,
Jul 15, 2008, 5:17:23 AM7/15/08
to Django users
Hi
I got the same issue.

from django.test import TestCase

class AccountViewTest(TestCase):
def test_account_register(self):
user_data_dict={
'username':'userwerthrthh',
'email':'ro...@localhost.localdomain.com',
'password':'psdfsdppps',
'password2':'psdfsdppps',
'user_group_id':2,
'name':'Test User',
'nip':4918851570,
'regon':970931428,
'description':'rtrt rty y',
'city':'sdfg dfggh',
'street':'fdsf dsfg sdfg',
'postcode':111,
}
self.client.post('/account/register/', user_data_dict)


----------------------------------------------------------------------
account/tests.py", line 37, in test_account_register
self.client.post('/account/register/', user_data_dict)
File "/usr/lib/python2.5/site-packages/django/test/client.py", line
243, in post
return self.request(**r)
File "/usr/lib/python2.5/site-packages/django/test/client.py", line
171, in request
response = self.handler(environ)
File "/usr/lib/python2.5/site-packages/django/test/client.py", line
40, in __call__
response = self.get_response(request)
File "/usr/lib/python2.5/site-packages/django/core/handlers/
base.py", line 126, in get_response
subject = 'Error (%s IP): %s' % ((request.META.get('REMOTE_ADDR')
in settings.INTERNAL_IPS and 'internal' or 'EXTERNAL'), request.path)
TypeError: 'in <string>' requires string as left operand

----------------------------------------------------------------------

If I change a key called 'street' to 'whotever' or replace @ in email
value it works.
any ideas why?

cheers,
Paul

bshaurette

unread,
Jul 15, 2008, 2:54:22 PM7/15/08
to Django users
For what it's worth, I finally came up with a simple working test for
views. My tests were written correctly all along, I just had to take
care of those errors related to the project settings. It never
occurred me to just set constants at the top of the test file, but
that's what worked.


import os
from django.test import Client, TestCase

os.environ['MEDIA_LOCATION'] = '/Users/bshaurette/myproject/public'
INTERNAL_IPS = ('127.0.0.1',)

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

def test_login(self):
response = self.client.post('/accounts/login/', {'username':
'bshaurette', 'password': 'bshaurette'})
self.assertEqual(response.status_code, 200)

def tearDown(self):
self.client.post('/accounts/logout/')


(we have a pattern in the base project urls.py that looks for that env
variable 'MEDIA_LOCATION' as the location for site media ...
hence ... )

pgb

unread,
Jul 16, 2008, 2:32:45 AM7/16/08
to Django users

On 15 Lip, 20:54, bshaurette <bshaure...@gmail.com> wrote:
> For what it's worth, I finally came up with a simple working test forviews.  Mytestswere written correctly all along, I just had to take
> care of those errors related to the project settings.  It never
> occurred me to just set constants at the top of the test file, but
> that's what worked.
>
> import os
> from django.test import Client, TestCase
>
> os.environ['MEDIA_LOCATION'] = '/Users/bshaurette/myproject/public'
> INTERNAL_IPS = ('127.0.0.1',)

Thanks for the info and your help.
I also added:
self.client.defaults={'REMOTE_ADDR':'localhost'}
to my setUp method.

I think it would be a good idea if web client could set those vars for
us as default on startup.

> class ViewTests(TestCase):
>     def setUp(self):
>         self.client = Client()
>
>     def test_login(self):
>         response = self.client.post('/accounts/login/', {'username':
> 'bshaurette', 'password': 'bshaurette'})
>         self.assertEqual(response.status_code, 200)
>
>     def tearDown(self):
>         self.client.post('/accounts/logout/')
>
> (we have a pattern in the base project urls.py that looks for that env
> variable 'MEDIA_LOCATION' as the location for site media ...
> hence ... )

Many thanks
Paul Bielecki
Reply all
Reply to author
Forward
0 new messages