Every time I run a test all my database tables (except for the migrations table) are being deleted and I have to run the migrations again. For instance, if I have the following tables:
migrations
users
tableA
tableB
after running:
phpunit --filter user_can_view_a_record ViewRecordTest tests/Feature/ViewRecordTest.php
my tables are deleted and I end up with just the migrations table.
I'm using MySQL as database and according to the configuration I have set up the tests are being running in memory:
database.php
'connections' => [
'sqlite' => [
'driver' => 'sqlite',
//'database' => env('DB_DATABASE', database_path('database.sqlite')),
'database' => ':memory:',
'prefix' => '',
],
'mysql' => [
'driver' => 'mysql',
'host' => env('DB_HOST', '127.0.0.1'),
'port' => env('DB_PORT', '3306'),
'database' => env('DB_DATABASE', 'forge'),
'username' => env('DB_USERNAME', 'forge'),
'password' => env('DB_PASSWORD', ''),
'unix_socket' => env('DB_SOCKET', ''),
'charset' => 'utf8mb4',
'collation' => 'utf8mb4_unicode_ci',
'prefix' => '',
'strict' => false,
'engine' => null,
'sticky' => true
],
]
phpunit.xml
<?xml version="1.0" encoding="UTF-8"?>
<phpunit backupGlobals="false"
backupStaticAttributes="false"
bootstrap="vendor/autoload.php"
colors="true"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
processIsolation="false"
stopOnFailure="false">
<testsuites>
<testsuite name="Feature">
<directory suffix="Test.php">./tests/Feature</directory>
</testsuite>
<testsuite name="Unit">
<directory suffix="Test.php">./tests/Unit</directory>
</testsuite>
</testsuites>
<filter>
<whitelist processUncoveredFilesFromWhitelist="true">
<directory suffix=".php">./app</directory>
</whitelist>
</filter>
<php>
<env name="APP_ENV" value="testing"/>
<env name="CACHE_DRIVER" value="array"/>
<env name="SESSION_DRIVER" value="array"/>
<env name="QUEUE_DRIVER" value="sync"/>
<env name="DB_CONNECTION" value="sqlite" />
<env name="DB_DATABASE" value=":memory:" />
</php>
</phpunit>
Thanks
the solution of Michael Ameyaw worked well for me in PHPStorm ! thank you.
I wanted to mention that it could be also a cache problem, so try this:
php artisan config:clear
In general you should have separated database with same table structure like staging env. It is normal behavior of unit tests (I mean deleting of tables). Typical flow for testing is (for each test execution):
Imagine situation, that one of tests change data in for example user table (change email or first name etc) if phpunit don't drop all data next tests will work with incorrect data ( changed by another test). You can check laravel main documentation for more details.
I got a solution, if you are using phpStorm look for test framework.
File >> settings >> File & Framework >> Test Framework.
Set a default configuration file to phpunit.xml in your root folder
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With