In the Laravel app, we must test our application’s behaviors across various input and output scenarios. Laravel provides a powerful and expressive API for HTTP testing, making it easy to ensure your application’s routes, controllers, middleware, and responses work as expected.

Whether you’re testing a REST API or a web app with forms, this cheat sheet will help you quickly find the right syntax and best practices.
Prerequirement
For that, we need a well-developed Laravel application.
Laravel HTTP Testing Cheat Sheet
Setup: Create a Test file
$ php artisan make:test UserControllerTest
You’ll find it in tests/Feature/UserControllerTest.php
Basic HTTP Request Testing
public function test_homepage_loads()
{
$response = $this->get('/');
$response->assertStatus(200);
$response->assertSee('Welcome');
}
POST Request Testing
public function test_user_can_register()
{
$response = $this->post('/register', [
'name' => 'Jane Doe',
'email' => 'jane@example.com',
'password' => 'secret123',
'password_confirmation' => 'secret123',
]);
$response->assertRedirect('/home');
$this->assertDatabaseHas('users', [
'email' => 'jane@example.com',
]);
}
Authenticated Requests
public function test_authenticated_user_can_access_dashboard()
{
$user = User::factory()->create();
$response = $this->actingAs($user)->get('/dashboard');
$response->assertStatus(200);
$response->assertSee('Dashboard');
}
Validation Testing
public function test_registration_requires_email()
{
$response = $this->post('/register', [
'name' => 'No Email',
'password' => 'secret',
'password_confirmation' => 'secret',
]);
$response->assertSessionHasErrors('email');
}
Testing JSON APIs
public function test_api_returns_users()
{
User::factory()->count(3)->create();
$response = $this->getJson('/api/users');
$response->assertStatus(200)
->assertJsonCount(3)
->assertJsonStructure([
'*' => ['id', 'name', 'email'],
]);
}
Redirects & Middleware Testing
public function test_guest_is_redirected_from_dashboard()
{
$response = $this->get('/dashboard');
$response->assertRedirect('/login');
}
Session, Flash, and Old Input
public function test_session_flash_message()
{
$response = $this->withSession(['success' => 'Profile updated'])
->get('/profile');
$response->assertSee('Profile updated');
}
Conclusion:
Whether you’re testing web pages, APIs, or form submissions, Laravel makes it easy. It lets you pretend to send requests, check JSON responses, work with session data, and even make sure the database has the right info. This helps you make sure your app works the way it should—before your users ever touch it.
