manhnguyen's avatar

RefreshDatabase when testing using MySQL

I'm using MySQL as DB for testing because I want the same environment as my real server. But I realize that DB is not refreshed after each test classes (It only be refreshed if I use the Sqlite). When I run seeder in a test class, the data will be remain when another test class runs.

Regarding that, I think about 2 approaches:

  1. Run a seeder before all test classes, but I can't find a hook to do that (the setUp or setUpBeforeClass don't work because I need to run before all classes, not each class or each test).
  2. Find a way to refresh the DB after each test class, so I can use the setUp function to seed the data again.

Which one is possible and better?

Thank you all for reading!

0 likes
1 reply
Tray2's avatar

Laravel uses something called transactions when it runs the tests, that means that it migrates the database once, then it starts a transaction, runs the test, and does a rollback after each test. This gives the illusion of the database being refreshed between every test, the side effect is that the auto incrementing id's continue to increment between each test, that means that you can't write tests that uses a hard coded id in the assertions.

Please or to participate in this conversation.