setting up test environment laravel

  laravel, php, testing

I recently set up a new laravel 8 project and I want to run the tests in memory so I set the testing environment to the following:

  • in phpunit.xml I set db
<server name="DB_CONNECTION" value="sqlite"/>
<server name="DB_DATABASE" value=":memory:"/>
  • Database connection config/database.php
'sqlite' => [
            'driver' => 'sqlite',
            'url' => env('DATABASE_URL'),
            'database' => env('DB_DATABASE', database_path('database.sqlite')),
            'prefix' => '',
            'foreign_key_constraints' => env('DB_FOREIGN_KEYS', true),
  • .env.testing
APP_NAME="Laravel 8 Project"


  • in test class:

namespace TestsFeature;

use AppModelsUser;
use IlluminateFoundationTestingRefreshDatabase;
use LivewireLivewire;
use TestsTestCase;

class ContactFormComponentTest extends TestCase
    use RefreshDatabase;

    /** @test  */
    function create_new_user()
        $user = new User();
        $user->name = 'john doe';
        $user->email = '[email protected]';
        $user->password = 'password';

        $savedUser = $user->save();


I also ran:

php artisan config:cache
php artisan cache:clear

After all of this when I run php artisan test --filter create_new_user the test pass but it clears my real db instead of using :memory:

Any Advice,

Source: Laravel

Leave a Reply