I have many, many migrations I can't delete. So when I run the tests it takes too much time unless I run them with --keepdb which is perfect. The only question is how does --keepdb and the setUp method work together. In the setUp method of the test I do something like this:
class APITests(APITestCase):
fixtures = ['tests/testdata.json']
def setUp(self):
username = "test"
password = "1234"
user_created = User.objects.create_user(username=username, password=password)
body = {
"username": username,
"password": password
}
cart = Cart.objects.create()
Client.objects.create(user=user_created, cart=cart)
APITestCase is just a django rest framework wrapper for the django test class. I create a user, a client and a cart for that user. If I re-run the tests with --keepdb, will the setUp method create a duplicated user or cart? how does it work in this case?
The setUp method runs before every test case in your
APITests. Because each test runs in a transaction, any objects created are destroyed at the end of each test.The
keepdboption does not affect this at all.You might be able to use
setupTestDatato make your tests more efficient.