[Django]-Django, PostgreSQL, set_autocommit and test cases

5👍

Django TestCase inherits from TransactionTestCase.

According to the doc, TestCase does basically the same as TransactionTestCase, but surrounds every test with a transaction (…). You have to use TransactionTestCase, if you need transaction management inside a test.

My situation is a little bit different because my test class is derived from DRF APITestCase. So in order to check transaction management in my test case, I did the following:

from rest_framework.test import APITestCase
from django.test import TestCase

class MyTestCase(APITestCase):

    def _fixture_setup(self):
        super(TestCase, self)._fixture_setup()

    def _fixture_teardown(self):
        super(TestCase, self)._fixture_teardown()

    ...

Leave a comment