Truncate is the fastest to use for testing #3268
-
DatabaseMigration is way too slow for testing takes 3 to 5 seconds for each test, RefreshDatabase wont work if you test a function with transaction since nested transaction is not allowed. Looking at the test technique made by @GromNaN in this package, truncate is being used to make testing faster which I also tried and tested and shave off the time to average 0.2 seconds to execute each test. What could be the reason why DatabaseMigration takes a long time to test? |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 6 replies
-
You're right that deleting/creating a database takes longer than simply deleting all the documents in the collections. However, you still need to consider the insertion of fixtures between each test. Perhaps you could optimize this by dumping/importing with bulkWrite instead of unit insertions. Another common technique on advanced databases is to use a transaction that is reverted at the end of each test. This also works with MongoDB. In
In
|
Beta Was this translation helpful? Give feedback.
-
Just additional feedback it might be the fastest to delete collections or truncate but when you try to do parallel testing you might need DatabaseMigration or RefreshDatabase for it to work and make laravel auto create additional databases. But even if you parallel test, the sequential test for truncate is still faster. Truncate strategy sequential, doesnt work with parallel test
RefreshDatabase sequential(this works as long as you are testing doesn't do transactions, since by itself its transaction which mongodb doesnt allow nested one)
RefreshDatabase parallel
DatabaseMigration sequential
DatabaseMigration parallel
|
Beta Was this translation helpful? Give feedback.
You don't need to know about the models, you can list all the collections of your database and delete all the documents they contain.
Don't drop the collections if you use indexes or specific collections options like timeseries.