Account Options

  1. Sign in
The old Google Groups will be going away soon.
Switch to the new Google Groups.
Google Groups Home
« Groups Home
writing unit tests for views?
There are currently too many topics in this group that display first. To make this topic appear first, remove this option from another topic.
There was an error processing your request. Please try again.
flag
  5 messages - Collapse all  -  Translate all to Translated (View all originals)
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
 
From:
To:
Cc:
Followup To:
Add Cc | Add Followup-to | Edit Subject
Subject:
Validation:
For verification purposes please type the characters you see in the picture below or the numbers you hear by clicking the accessibility icon. Listen and type the numbers you hear
 
bshaurette  
View profile  
 More options Jul 14 2008, 8:28 pm
From: bshaurette <bshaure...@gmail.com>
Date: Mon, 14 Jul 2008 17:28:31 -0700 (PDT)
Local: Mon, Jul 14 2008 8:28 pm
Subject: writing unit tests for views?
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?


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Russell Keith-Magee  
View profile  
 More options Jul 14 2008, 10:17 pm
From: "Russell Keith-Magee" <freakboy3...@gmail.com>
Date: Tue, 15 Jul 2008 10:17:11 +0800
Local: Mon, Jul 14 2008 10:17 pm
Subject: Re: writing unit tests for views?

On Tue, Jul 15, 2008 at 8:28 AM, bshaurette <bshaure...@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 %-)


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
pgb  
View profile  
 More options Jul 15 2008, 5:17 am
From: pgb <pawciob...@gmail.com>
Date: Tue, 15 Jul 2008 02:17:23 -0700 (PDT)
Local: Tues, Jul 15 2008 5:17 am
Subject: Re: writing unit tests for views?
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':'r...@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

On 15 Lip, 02:28, bshaurette <bshaure...@gmail.com> wrote:


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
bshaurette  
View profile  
 More options Jul 15 2008, 2:54 pm
From: bshaurette <bshaure...@gmail.com>
Date: Tue, 15 Jul 2008 11:54:22 -0700 (PDT)
Local: Tues, Jul 15 2008 2:54 pm
Subject: Re: writing unit tests for views?
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 ... )


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
pgb  
View profile  
 More options Jul 16 2008, 2:32 am
From: pgb <pawciob...@gmail.com>
Date: Tue, 15 Jul 2008 23:32:45 -0700 (PDT)
Local: Wed, Jul 16 2008 2:32 am
Subject: Re: writing unit tests for views?

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

 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
End of messages
« Back to Discussions « Newer topic     Older topic »