title: Rollback Many Transactions between tests in Flask
body: <p>My tests take a long time to run and I am trying to rollback transactions between tests instead of dropping and creating the tables between tests.</p>
  
  <p>The issues is that in some tests I do multiple commits.</p>
  
  <p><strong>EDIT: How do I rollback transactions between tests so that tests will run faster</strong></p>
  
  <p>Here is the Base class used for testing.</p>
  
  <pre><code>import unittest
  from app import create_app
  from app.core import db
  from test_client import TestClient, TestResponse
  
  
  class TestBase(unittest.TestCase):
      def setUp(self):
          self.app = create_app('testing')
          self.app_context = self.app.app_context()
          self.app_context.push()
          self.app.response_class = TestResponse
          self.app.test_client_class = TestClient
          db.create_all()
  
      def tearDown(self):
          db.session.remove()
          db.drop_all()
          db.get_engine(self.app).dispose()
          self.app_context.pop()
  </code></pre>
  
  <p>Here is my attempt at rolling back transactions.</p>
  
  <pre><code>class TestBase(unittest.TestCase):
      @classmethod
      def setUpClass(cls):
          cls.app = create_app('testing')
          cls.app_context = cls.app.app_context()
          cls.app_context.push()
          cls.app.response_class = TestResponse
          cls.app.test_client_class = TestClient
  
          db.create_all()
  
      @classmethod
      def tearDown(cls):
          db.session.remove()
          db.drop_all()
          db.get_engine(cls.app).dispose()
  
      def setUp(self):
          self.app_content = self.app.app_context()
          self.app_content.push()
          db.session.begin(subtransactions=True)
  
      def tearDown(self):
          db.session.rollback()
          db.session.close()
  
          self.app_context.pop()
  </code></pre>