django tests

61 views
Skip to first unread message

Harjot Mann

unread,
Nov 21, 2013, 11:16:42 PM11/21/13
to django...@googlegroups.com
I am creating tests for my django app. Here is my test and I am
getting an error.
Here is my test code.
UserProfile is my model name and profile is my view corresponding to that.

http://tny.cz/82ce97ef

This is the error I am getting
ERROR: test_profile (Automation.tcc.tests.UserProfile)
----------------------------------------------------------------------
Traceback (most recent call last):
File "/home/harjot/Automation/../Automation/tcc/tests.py", line 15,
in test_profile
self.assertEqual(self.first_name, "Harjot")
AttributeError: 'UserProfile' object has no attribute 'first_name'

Help me please.

--
Harjot Kaur Mann
Blog: http://harjotmann.wordpress.com/
Daily Dairy: http://harjotmann.wordpress.com/daily-diary/

Charly Román

unread,
Nov 22, 2013, 12:47:59 AM11/22/13
to django...@googlegroups.com
Your testcase's name and model's name its the same, fix this.

2013/11/21 Harjot Mann <harjotm...@gmail.com>:
> --
> You received this message because you are subscribed to the Google Groups "Django users" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
> To post to this group, send email to django...@googlegroups.com.
> Visit this group at http://groups.google.com/group/django-users.
> To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CAB0GQhCWHBLq97OCDY9FrDfR6zRD08_1-zcr6bqN5EP01OGYcQ%40mail.gmail.com.
> For more options, visit https://groups.google.com/groups/opt_out.

Harjot Mann

unread,
Nov 22, 2013, 1:17:50 AM11/22/13
to django...@googlegroups.com
On Fri, Nov 22, 2013 at 11:17 AM, Charly Román <chack...@gmail.com> wrote:
> Your testcase's name and model's name its the same, fix this.


Changed it. But still getting the same error. Now the code is this:
http://tny.cz/941266d9

Charly Román

unread,
Nov 22, 2013, 1:25:23 AM11/22/13
to django...@googlegroups.com
http://tny.cz/c883f980


You need to rea about OOP in Python.



2013/11/22 Harjot Mann <harjotm...@gmail.com>:
> --
> You received this message because you are subscribed to the Google Groups "Django users" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
> To post to this group, send email to django...@googlegroups.com.
> Visit this group at http://groups.google.com/group/django-users.
> To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CAB0GQhBYbrkqPsskUKg5j9xVAKJZMMPxiKYKx2TL_ckwN1qJDw%40mail.gmail.com.

Charly Román

unread,
Nov 22, 2013, 1:25:44 AM11/22/13
to django...@googlegroups.com
http://tny.cz/c883f980


You need to read about OOP in Python.

2013/11/22 Charly Román <chack...@gmail.com>:

Xavier Ordoquy

unread,
Nov 22, 2013, 1:43:42 AM11/22/13
to django...@googlegroups.com
Hi,

I doubt the error is the same although it might look similar.
Please note that TestCase use setUp instead of setup.

Regards,
Xavier Ordoquy,
Linovia.
> --
> You received this message because you are subscribed to the Google Groups "Django users" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
> To post to this group, send email to django...@googlegroups.com.
> Visit this group at http://groups.google.com/group/django-users.
> To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CAB0GQhBYbrkqPsskUKg5j9xVAKJZMMPxiKYKx2TL_ckwN1qJDw%40mail.gmail.com.

Harjot Mann

unread,
Nov 22, 2013, 2:14:18 AM11/22/13
to django...@googlegroups.com
On Fri, Nov 22, 2013 at 12:13 PM, Xavier Ordoquy <xord...@linovia.com> wrote:
> I doubt the error is the same although it might look similar.
> Please note that TestCase use setUp instead of setup.


Oh thankyou. But now I am getting a long list of errors.
http://tny.cz/c4018552

Charly Román

unread,
Nov 22, 2013, 2:20:47 AM11/22/13
to django...@googlegroups.com
It's because you have fields in your model that aren't optional, and
in your code you only set one field.



2013/11/22 Harjot Mann <harjotm...@gmail.com>:
> --
> You received this message because you are subscribed to the Google Groups "Django users" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
> To post to this group, send email to django...@googlegroups.com.
> Visit this group at http://groups.google.com/group/django-users.
> To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CAB0GQhAD23Sj8OpXQF2H0UAUFyacMxP%3DHj_m2peG8LjqA9%2BSUA%40mail.gmail.com.

Harjot Mann

unread,
Nov 22, 2013, 2:35:42 AM11/22/13
to django...@googlegroups.com
On Fri, Nov 22, 2013 at 12:50 PM, Charly Román <chack...@gmail.com> wrote:
> It's because you have fields in your model that aren't optional, and
> in your code you only set one field.


Ok so I need to give alll the fields and it is having foreign key
constraint I think, so for that I need to use that also?

Charly Román

unread,
Nov 22, 2013, 2:43:04 AM11/22/13
to django...@googlegroups.com
Yes, you need to give all the fields that arent't optional. Included
foreign keys.

2013/11/22 Harjot Mann <harjotm...@gmail.com>:
> --
> You received this message because you are subscribed to the Google Groups "Django users" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
> To post to this group, send email to django...@googlegroups.com.
> Visit this group at http://groups.google.com/group/django-users.
> To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CAB0GQhASUuaAhr9DqyU3---6snn3tS1eJPvVgsSMSc_a6%3DYarQ%40mail.gmail.com.

Harjot Mann

unread,
Nov 22, 2013, 2:49:54 AM11/22/13
to django...@googlegroups.com
On Fri, Nov 22, 2013 at 1:13 PM, Charly Román <chack...@gmail.com> wrote:
> Yes, you need to give all the fields that arent't optional. Included
> foreign keys.


Not optional means that are not validated. If yes why?
I mean these are the compulsory fields, so why we should not give it.
I used all the compulsory fields but still I am getting the same
error. And how we can use foreign key in tests.py file ?

Xavier Ordoquy

unread,
Nov 22, 2013, 3:17:04 AM11/22/13
to django...@googlegroups.com
Hi,

I feel like you are trying to get too fast into Django.
It looks like you should take some time and read the Django’s documentation. It is written like a book and would give an accurate view of what’s going on.
If things are still not clear after that, we’d be interested to hear from it and understand what didn’t go well so that we could improve the documentation.

Regards,
Xavier Ordoquy,
Linovia.

Harjot Mann

unread,
Nov 22, 2013, 3:26:05 AM11/22/13
to django...@googlegroups.com
On Fri, Nov 22, 2013 at 1:47 PM, Xavier Ordoquy <xord...@linovia.com> wrote:
> I feel like you are trying to get too fast into Django.
> It looks like you should take some time and read the Django’s documentation. It is written like a book and would give an accurate view of what’s going on.
> If things are still not clear after that, we’d be interested to hear from it and understand what didn’t go well so that we could improve the documentation.


I have read the documentation and doing according to it. But in
testing doucumentation, it is not given how to use the foreign key.

Xavier Ordoquy

unread,
Nov 22, 2013, 4:34:34 AM11/22/13
to django...@googlegroups.com
Hi,

Tests are regular Python code using Django. The code shouldn’t be different from what you write elsewhere.
You’ll find some example about using foreign key by the end of the first tutorial page.
In your case, it looks like you’ll need to create a user and add it to the model you are testing since they seem to be mandatory.

Regards,
Xavier Ordoquy,
Linovia.
> --
> You received this message because you are subscribed to the Google Groups "Django users" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
> To post to this group, send email to django...@googlegroups.com.
> Visit this group at http://groups.google.com/group/django-users.
> To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CAB0GQhBJLDmQTHt3JmjVE77vaWneF-tH6mijFdTLmez80ODL%2BA%40mail.gmail.com.

Arun K Reddy

unread,
Nov 22, 2013, 9:23:22 PM11/22/13
to django...@googlegroups.com
Simple, first create a user model and do the following:
 
from django.contrib.auth import User

 class UserProfileTestCase(TestCase):
        def setup(self):
                self.user = User.objects.filter(pk=1)
               
self.userprofile = UserProfile.objects.create(first_name = "Harjot")
       
def test_profile(self):
               
self.assertEqual(self.userprofile.first_name, "Harjot")

add any other not optional fields. Hope it works.

Harjot Mann

unread,
Nov 23, 2013, 3:38:59 AM11/23/13
to django...@googlegroups.com
On Sat, Nov 23, 2013 at 7:53 AM, Arun K Reddy <arun...@gmail.com> wrote:
> add any other not optional fields. Hope it works.


Same error. Even on adding
from django.contrib.auth import User
Import error is giving.

Harjot Mann

unread,
Nov 23, 2013, 3:40:25 AM11/23/13
to django...@googlegroups.com
Now my code is this:
class UserProfileTestCase(TestCase):
def setUp(self):
self.userprofile = UserProfile.objects.filter(user_id =
1).update(first_name = "Harjot")

def test_profile(self):
self.assertEqual(self.userprofile.user_id, 1)
and I am getting this error:
Traceback (most recent call last):
File "/home/harjot/Automation/../Automation/tcc/tests.py", line 20,
in test_profile
self.assertEqual(self.userprofile.user_id, 1)
AttributeError: 'int' object has no attribute 'user_id'

Simone Dalla

unread,
Nov 23, 2013, 4:19:35 AM11/23/13
to Michele Magazz



2013/11/23 Harjot Mann <harjotm...@gmail.com>

Now my code is this:
class UserProfileTestCase(TestCase):
    def setUp(self):
        self.userprofile = UserProfile.objects.filter(user_id =
1).update(first_name = "Harjot")

    def test_profile(self):
        self.assertEqual(self.userprofile.user_id, 1)
and I am getting this error:
Traceback (most recent call last):
  File "/home/harjot/Automation/../Automation/tcc/tests.py", line 20,
in test_profile
    self.assertEqual(self.userprofile.user_id, 1)
AttributeError: 'int' object has no attribute 'user_id'


The method update return the number of rows matched (read the django docs at https://docs.djangoproject.com/en/1.5/ref/models/querysets/#update) not objects updated. Your code:

"self.userprofile = UserProfile.objects.filter(user_id =1).update(first_name = "Harjot")"

set the variable "self.userprofile" with the number of rows matched by "UserProfile.objects.filter(user_id =1)", 
therefore "self.userprofile" is an int object as the exception is telling you ("AttributeError: 'int' object has no attribute 'user_id'")

the correct code should be

...
self.userprofile = UserProfile.objects.filter(user_id =1)
self.userprofile.update(first_name = "Harjot")
...
self.assertEqual(self.userprofile.user_id, 1)

Cheers,
Simone

--
Simo

- Registered Linux User #395060

- Software is like sex, it is better when it is free  --> Linus B. Torvalds

Harjot Mann

unread,
Nov 23, 2013, 7:30:40 AM11/23/13
to django...@googlegroups.com
On Sat, Nov 23, 2013 at 2:49 PM, Simone Dalla <simo...@gmail.com> wrote:
> the correct code should be
>
> ...
> self.userprofile = UserProfile.objects.filter(user_id =1)
> self.userprofile.update(first_name = "Harjot")
> ...
> self.assertEqual(self.userprofile.user_id, 1)

New Error.
Traceback (most recent call last):
File "/home/harjot/Automation/../Automation/tcc/tests.py", line 22,
in test_profile
self.assertEqual(self.userprofile.user_id, 1)
AttributeError: 'QuerySet' object has no attribute 'user_id'

Felipe Coelho

unread,
Nov 23, 2013, 11:12:07 AM11/23/13
to Django users
2013/11/23 Harjot Mann <harjotm...@gmail.com>

On Sat, Nov 23, 2013 at 2:49 PM, Simone Dalla <simo...@gmail.com> wrote:
> the correct code should be
>
> ...
> self.userprofile = UserProfile.objects.filter(user_id =1)
> self.userprofile.update(first_name = "Harjot")
> ...
> self.assertEqual(self.userprofile.user_id, 1)

New Error.
Traceback (most recent call last):
  File "/home/harjot/Automation/../Automation/tcc/tests.py", line 22,
in test_profile
    self.assertEqual(self.userprofile.user_id, 1)
AttributeError: 'QuerySet' object has no attribute 'user_id'

filter() [1] returns a QuerySet, if you want and are *absolutely sure* that your query will return a single object, use get() [2] instead, possibly with a try/catch block.

[1] https://docs.djangoproject.com/en/1.6/ref/models/querysets/#django.db.models.query.QuerySet.filter
[2] https://docs.djangoproject.com/en/1.6/ref/models/querysets/#django.db.models.query.QuerySet.get
Reply all
Reply to author
Forward
0 new messages