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
Illuminate\Foundation\Testing\RefreshDatabase;
? as documentation states? laravel.com/docs/5.5/… – Sartretesting
? I think phpunit.xml is not being used. You can do that viadd(app()->environment());
. – Sartrephpunit
command? I personally use this aliasphpunit=vendor/bin/phpunit
and it uses phpunit.xml by default. – Sartrephp artisan cache:clear
. Good Luck! :) – Sartrephp artisan config:clear
... – Sartre