Problems from writing test cases.

66 views
Skip to first unread message

ANi

unread,
Dec 10, 2018, 11:00:07 PM12/10/18
to Django users
Hello,
I am now trying to write test cases for my project, but I find some problems.

1.


class FirstTest(TestCase):

   
@classmethod
   
def setUpTestData(cls):
       
User.objects.create(username = 'johndoe', password = 'goodtobehere123')
       
...

   
def test_case_one(self):
       user
= User.objects.get(pk=1)
       
# some assertions here

       
...

class SecondTest(TestCase):

   
@classmethod
   
def setUpTestData(cls):
       
User.objects.create(username = 'janedoe', password = 'nicetobethere456')

   
def test_case_one(self):
       user
= User.objects.get(pk=1)
       
# some assertions here

       
...
   

If I run all the tests together, I can pass the FirstTest while getting error of "django.contrib.auth.models.DoesNotExist: User matching query does not exist." on the SecondTest.
If I run two test cases separately, all tests are passed.
Then solved it by changing it into this:


class FirstTest(TestCase):

   
@classmethod
   
def setUpTestData(cls):
        cls
.user = User.objects.create(username = 'johndoe', password = 'goodtobehere123')
       
...

   
def test_case_one(self):
       
self.assertEqual(self.user.somefunc(), something)
       
# some assertions here

       
...

class SecondTest(TestCase):

   
@classmethod
   
def setUpTestData(cls):
       cls
.user = User.objects.create(username = 'janedoe', password = 'nicetobethere456')
       
...

   
def test_case_one(self):
       
self.assertEqual(self.user.somefunc(), something)
       
# some assertions here

       
...



However I don't know why.....?????


2.

class ViewTest(TestCase):
     
   
@classmethod
   
def setUpTestData(cls):
        cls
.user = User.objects.create(username = 'johndoe', password = 'goodtobehere123')
       
...

     
def setUp(self):
         
self.item = Item.objects.create(
             category
= 1,
             item_content
= 'content content',
         )


     
def test_item_update(self):
     # the view will update the item and return HTTP status code 200.
     
# if the item is not exist, raise Http404  
         
self.c = Client()
         resp
= self.c.post(
            reverse
('update_item', kwargs={"pk":self.item.pk}),
            data
= {
               
"category": 2,
               
"content": "item content",
             
}
         
)

         
self.assertEqual(resp.status_code, 200)
         
self.assertEqual(self.item.category, 2)

     
def test_item_delete(self):  
     # the view will delete the item and return HTTP status code 200.
     
# if the item is not exist, raise Http404  
         self.c = Client()
         resp
= self.c.post(
            reverse
('delete_item'),
            data
= {
               
"pk": self.item.pk
             
}
         
)

         
self.assertEqual(resp.status_code, 200)
 



I got " AssertionError: 1 != 2 ". for test_item_update()
and "AssertionError: 404 != 200". for test_item_delete()

I think it is reasonable but obviously I misunderstand something. 

Thank you. I will be very happy if you want to help me >___<



Yarving Liu

unread,
Dec 10, 2018, 11:08:44 PM12/10/18
to django...@googlegroups.com
For the:
self.assertEqual(self.item.category, 2)

this is because you defined self.item in setUp, and self.category = 1.


Others have no idea, expect answers the same.

--
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 https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/f1f9855a-3461-45a1-bdee-2ec901a7f718%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

ANi

unread,
Dec 10, 2018, 11:45:01 PM12/10/18
to Django users
Oh. ok :)

Then I tried to call again the item object by its pk, and it works.
item = Item.objects.get(pk=self.item.pk)

and sorry for the test_item_delete(). 
I passed the wrong parameter so the view got a None to retrieve the item object, that's why it returns 404.
 

thank you so much.

Yarving Liu

unread,
Dec 11, 2018, 9:42:35 AM12/11/18
to django...@googlegroups.com
I never tried UT like below:
class SecondTest(TestCase):

    
@classmethod
    
def setUpTestData(cls):
        
User.objects.create(username = 'janedoe', password = 'nicetobethere456')


Thank you the same.

--
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 https://groups.google.com/group/django-users.

ANi

unread,
Dec 13, 2018, 2:25:09 AM12/13/18
to Django users
This is my first time. lol

I thought we can access the data by using ORM as we usually do rather than set it to a class variable. 

nm

unread,
Dec 13, 2018, 9:25:36 AM12/13/18
to Django users
Regarding the first question and the problem with retrieving the user with `pk=1`:
When you run your tests, Django creates a test database where all the objects created by the test cases are stored, and which is destroyed after all the tests are run. Each user you create gets a *new* pk. The pk number is always incremented, regardless of whether the user with a previous pk was deleted in the meantime or not (btw, this works the same in your "regular" database, you can try adding and deleting users in the shell and see yourself). So when you run both test cases and in each test case you create one user, one of them gets `pk=1`, and the other `pk=2`. However, if you use the `setUpTestData` method in a test case, all the objects created by this methods are deleted after *this test case* finishes. This means that the first test case creates a user (pk=1), then this user is deleted, and the second test case creates another user (pk=2). Then you try to retrieve the user with pk=1, which fails.

By the way, the order in which tests are run is not always the same, so you should not rely on it in your tests. Assigning created object to class attributes, as you did later, is a much better approach.

Hope this is what you wanted to know (;
Message has been deleted

ANi

unread,
Dec 13, 2018, 8:13:06 PM12/13/18
to Django users
Great! Thank you for the explanation, it's what exactly I want to know, really helps a lot. ;-)

 

nm於 2018年12月13日星期四 UTC+8下午10時25分36秒寫道:
Reply all
Reply to author
Forward
0 new messages