2 ms·
Yes, this is slow because of how many database roundtrips the author is doing: @before_test def clean_session: for table in all_tables(): d
by herpderperator 5y ago
Yes, this is slow because of how many database roundtrips the author is doing:
@before_test
def clean_session:
for table in all_tables():
db.session.execute("truncate %s;" % table.name)
db.session.commit()
return db.session
But if you instead modify your before_suite (not before_test) flow to dynamically query the table names and subsequently create/update a stored procedure in the database that has your truncate queries in it:
delimiter //
create procedure truncate_tables()
begin
truncate table1;
truncate table2;
truncate table3;
truncate table4;
...etc...
end
delimiter ;
and then calling `db.session.execute("call truncate_tables()")` ONCE in your before_test instead, it will be orders of magnitudes faster.
I improved the test suite speed 15x at my previous job with this approach. We had a lot of tests and a lot of tables. O(n^2) to O(n)... :)