Delete DB records instead of tables to speedup UT
Now that the schema is fixed for all of the plugins, there isn't a need to delete and recreate the entire schema for every unit test. This patch clears the tables at the end of each test instead of deleting them. This eliminated overhead seems to save 10%+ execution time of the entire set of unit tests. Example of performance gain from tox -epy27 tests.unit.ml2: Before: Ran 2495 tests in 284.186s After: Ran 2495 tests in 223.299s Change-Id: Ic5bcbb0cf941e0745890abc776d719e58bb42e35
This commit is contained in:
parent
48ec1fbfc3
commit
34cf04a0ea
@ -55,17 +55,24 @@ def create_request(path, body, content_type, method='GET',
|
||||
|
||||
class SqlTestCase(base.BaseTestCase):
|
||||
|
||||
# flag to indicate that the models have been loaded
|
||||
_TABLES_ESTABLISHED = False
|
||||
|
||||
def setUp(self):
|
||||
super(SqlTestCase, self).setUp()
|
||||
# Register all data models
|
||||
engine = db_api.get_engine()
|
||||
model_base.BASEV2.metadata.create_all(engine)
|
||||
if not SqlTestCase._TABLES_ESTABLISHED:
|
||||
model_base.BASEV2.metadata.create_all(engine)
|
||||
SqlTestCase._TABLES_ESTABLISHED = True
|
||||
|
||||
def unregister_models():
|
||||
"""Unregister all data models."""
|
||||
model_base.BASEV2.metadata.drop_all(engine)
|
||||
def clear_tables():
|
||||
with engine.begin() as conn:
|
||||
for table in reversed(
|
||||
model_base.BASEV2.metadata.sorted_tables):
|
||||
conn.execute(table.delete())
|
||||
|
||||
self.addCleanup(unregister_models)
|
||||
self.addCleanup(clear_tables)
|
||||
|
||||
|
||||
class WebTestCase(SqlTestCase):
|
||||
|
Loading…
x
Reference in New Issue
Block a user