aloea
aloea

Reputation: 211

django setUpTestData doesn't run

Here is my test:

class FirstTestCase(TransactionTestCase):
    @classmethod
    def setUpTestData(cls):
        Car.objects.create(id=10001)

    def findAllCars(self):
        print(list(Car.objects.all()))

this gives no errors, and the list is simply printed as [], when clearly it should be [10001]

I know tests aren't supposed to inlude print statements, this is just an easy way to illustrate that for some reason the object in setUpTestData is not being created. How can I get it to make this object?

Upvotes: 3

Views: 550

Answers (1)

samwise
samwise

Reputation: 2257

setUpTestData is only run, if your test inherits from APITestCase but not from APITransactionTestCase:

from rest_framework.test import APITestCase


class ExampleTestCase(APITestCase):

    @classmethod
    def setUpTestData(cls):
        print("This is setUpTestData")

    def test_something(self):
        print("This is the test")
        self.assertTrue(True)

Upvotes: 2

Related Questions