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 (; W dniu wtorek, 11 grudnia 2018 05:00:07 UTC+1 użytkownik ANi napisał: > > 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/4047341b-88a8-4d56-9e01-cd5c0b3f6102%40googlegroups.com. For more options, visit https://groups.google.com/d/optout.