Testing
RefreshDatabase
Database tests start from a clean slate so one test never leaks rows into the next. The RefreshDatabase trait migrates a fresh test database before each case and rolls everything back after it. Add the single line to any test class that touches the database.
use Illuminate\Foundation\Testing\RefreshDatabase; class HomeTest extends TestCase { use RefreshDatabase; }
Write a test
A feature test is a normal class with methods that start with test. Call a route with get or post, then assert what the response should look like. Small focused methods beat one giant test every time.
class HomeTest extends TestCase { public function test_homepage_opens(): void { $response = $this->get("/"); $response->assertStatus(200); } }
Run it
One artisan command runs the whole suite and prints a short report per file. Run it after every change, and read failures from top to bottom because the first one often explains the rest.
php artisan test
PASS Tests\Feature\HomeTest Tests: 1 passed
Assert the database
HTTP asserts only check the response, so confirm stored data too. The assertDatabaseHas method checks that a table holds a row with the given values, and assertDatabaseMissing checks the opposite.
class SignupTest extends TestCase { public function test_signup_stores_user(): void { $this->assertDatabaseHas("users", [ "email" => "ada@example.com", ]); } }
Next: HTTP tests
Article author: Arthur Isaev