How to run feature tests in Laravel 9 in memory using SQLite

One of the advantages you get by using Laravel’s JetStream package is having built-in feature tests to check your authentication pages: Registration, profile information update, email verification, etc.

Simply running the php artisan test command will execute a bunch of tests (placed in tests/Feature folder) to make you ensure that you didn’t break main functionality while customizing the templates coming with Jetstream installation.

However, directly running these tests without a configuration will cause your local MySQL database to be reset. Thankfully, it’s easy to fix this behavior. Let’s fix it together.

First thing to do is to edit your phpunit.xml file (located in the project root) and uncomment two lines (line 24 and 25) as following:

<env name="DB_CONNECTION" value="sqlite"/>
<env name="DB_DATABASE" value=":memory:"/>

Second, edit the “tests/TestCase.php” file and create initDatabase & resetDatabase methods:

Now when you call the php artisan test command, PHPUnit will run all the tests in memory and won’t touch your local database.

That’s all!

Warning: Your config/database.php file should include an ‘sqlite’ entry in the connections. It should be there by default in Laravel 9. In case you have deleted it before, add it back:

'sqlite' => [
'driver' => 'sqlite',
'url' => env('DATABASE_URL'),
'database' => env('DB_DATABASE',database_path('database.sqlite')),
'prefix' => '',
'foreign_key_constraints' => env('DB_FOREIGN_KEYS', true)
]

--

--

--

Developer, Amateur Musician | Technical co-founder of off2class.com | Founder of valizim.com | Ex contributor @PCnetDergisi

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

REST API using Jersey 1.x

10 Best Java Frameworks to Use in 2021

Skills to Look Out For When Hiring a Software Developer

More on Unity

An Introduction to Sass and SCSS

JavaScript Buzzwords Part 1: AWS Lambda and Serverless

Create a Dynamic Field in Looker

How IntelliJ IDEA File Templates made my life easier as a developer.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Serdar Cevher

Serdar Cevher

Developer, Amateur Musician | Technical co-founder of off2class.com | Founder of valizim.com | Ex contributor @PCnetDergisi

More from Medium

How To Install Old Version Laravel Using Composer

Laravel Elasticsearch Integration Packages

Livewire — An Easy Way To Build Dynamic Web Applications (Part 2)

How to Add and Delete Rows Dynamically using jQuery

How to Add and Delete Rows Dynamically using jQuery