ahmadissa's avatar

Phpunit drops database tables

I'm trying to test my app using phpunit with laravel but each time I run

unit test

vendor\bin\phpunit

All tables in my database drop so I have to migrate each time I run a test

I tried mysql and sqlite and same issue

main database on mysql and test on sqlite

Have you encountered with similar issue & how can I fix it ?

0 likes
6 replies
5150studios's avatar

Hey dude, you probably have use Illuminate\Foundation\Testing\RefreshDatabase; in your imports. You can remove it. But it is a nice feature to have, just setup your seeders to mock out a production like database setup and it's really helpful to leave in.

shez1983's avatar
shez1983
Best Answer
Level 23

an other cool trick I do is - I create a test database (mysql) i seed it normally (by changing .env DB_DATABASE) and then use it in phpunit - this way i dont reseed/refresh it and it drastically speeds up my test - (I use the Transaction Trait in my test so each test is done in isolation & without changing the initial db seed state)

1 like
ahmadissa's avatar

No I'm not using RefreshDatabase

I found the issue its from "DatabaseMigrations"

when I used the DatabaseTransactions as @ shez1983 suggest everything worked.

I will try your way later i'm still in learning phase.

Thank you.

zhaozhenxiang's avatar

This is extremely dangerous. Unit testing should focus on the code. You can mock the data, but you can't delete the data or structure. I disagree. Also, I don't use migrations; I always manage my database manually.

Please or to participate in this conversation.