I am trying to find a way to rollback all database (mysql) changes (transactions) once after my tests finished. Not after each test method but after all of them done. I read that DatabaseTransactions
trait is used for this kind of approach but it rolls db transactions back after each test method. Operation going like:
Run Test1 -> Perform DB transactions -> Rollback -> Run Test2 ... -> Run LastTest
But what I need:
Run Test1 -> Perform DB transactions -> Run Test2 ..... -> Run LastTest -> Rollback
PS: I know I should use different database for testing and can use RefreshDatabase
trait for it. But in my case I can't use different database. Please don't ask why)
CodePudding user response:
For anyone who might get confused like me in the future I highly recommend to use Mocking for Unit testing to fake both your database and 3rd party APIs. After making some more research I have found that Unit tests should be independent from each other. They shouldn't affect database and 3rd party APIs shouldn't got called while testing.
For more understanding mocking
in laravel please check out the links:
https://laravel.com/docs/9.x/mocking
https://ralphjsmit.com/laravel-mock-dependencies
https://matthewdaly.co.uk/blog/2018/02/25/unit-testing-your-laravel-controllers