sqlalchemy - Rollback Many Transactions between tests in Flask -
sqlalchemy - Rollback Many Transactions between tests in Flask -
my tests take long time run , trying rollback transactions between tests instead of dropping , creating tables between tests.
the issues in tests multiple commits.
edit: how rollback transactions between tests tests run faster
here base of operations class used testing.
import unittest app import create_app app.core import db 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()
here effort @ rolling transactions.
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()
you utilize session.begin_nested
. long tests calling commit
close out sub-transactions, think can do
session.begin_nested() run_test(session) session.rollback()
which, in eyes, seems should faster. depends on database extent, however.
flask sqlalchemy flask-sqlalchemy
Comments
Post a Comment