Introduction
Testing helps catch mistakes early so your Laravel app works as expected. It makes your app more reliable and easier to fix.
Jump into concepts and practice - no test required
Testing helps catch mistakes early so your Laravel app works as expected. It makes your app more reliable and easier to fix.
public function test_example()
{
$response = $this->get('/');
$response->assertStatus(200);
}This is a simple Laravel test method inside a test class.
It uses $this->get() to request a page and checks the response status.
public function test_homepage_loads()
{
$response = $this->get('/');
$response->assertStatus(200);
}public function test_user_can_register()
{
$response = $this->post('/register', [
'name' => 'Alice',
'email' => 'alice@example.com',
'password' => 'secret123',
'password_confirmation' => 'secret123',
]);
$response->assertRedirect('/home');
}This test class has two tests. One checks the homepage returns status 200. The other checks the about page shows the text 'About Us'.
<?php namespace Tests\Feature; use Tests\TestCase; class BasicTest extends TestCase { public function test_homepage_returns_ok() { $response = $this->get('/'); $response->assertStatus(200); } public function test_about_page_contains_text() { $response = $this->get('/about'); $response->assertSee('About Us'); } }
Run tests with php artisan test in your Laravel project folder.
Tests help you find problems before users do.
Write tests for important parts of your app to keep it reliable.
Testing checks your app works as expected.
It helps catch bugs early and avoid surprises.
Laravel makes testing easy with built-in tools.
public function testHomePage() {
$response = $this->get('/');
$response->assertStatus(200);
}public function testUserCreation() {
$user = User::create(['name' => 'Alice']);
$this->assertDatabaseHas('users', ['name' => 'Alice']);
}