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 >___< -- 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+unsubscr...@googlegroups.com. To post to this group, send email to django-users@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.