Laravel provides a fluent API for making HTTP requests to your application and inspecting responses. No real HTTP server is needed — requests are simulated internally.
<?phptest('the application returns a successful response', function () { $response = $this->get('/'); $response->assertStatus(200);});
The get() method sends a GET request and assertStatus() verifies the HTTP status code of the response.
The CSRF middleware is automatically disabled during tests. You do not need to disable it manually.
Use get, post, put, patch, or delete to send requests in your tests. These methods return an Illuminate\Testing\TestResponse instance with many assertion helpers.
<?phptest('basic request', function () { $response = $this->get('/'); $response->assertStatus(200);});
Each test should generally make only one request. Making multiple requests in a single test method can cause unexpected behavior.
Use withSession() to seed the session with data before sending a request.
<?phptest('interacting with the session', function () { $response = $this->withSession(['banned' => false])->get('/');});
Use actingAs() to authenticate a user for the duration of the request. Combine it with model factories to create users on the fly.
<?phpuse App\Models\User;test('an action that requires authentication', function () { $user = User::factory()->create(); $response = $this->actingAs($user) ->withSession(['banned' => false]) ->get('/');});
Pass a guard name as the second argument to authenticate against a specific guard. The provided guard becomes the default for the duration of the test.
$this->actingAs($user, 'web');
To send an unauthenticated request, use actingAsGuest().
Use the Exceptions facade to assert that your application throws specific exceptions.
<?phpuse App\Exceptions\InvalidOrderException;use Illuminate\Support\Facades\Exceptions;test('exception is thrown', function () { Exceptions::fake(); $response = $this->get('/order/1'); // Assert an exception was thrown Exceptions::assertReported(InvalidOrderException::class); // Assert against the exception message Exceptions::assertReported(function (InvalidOrderException $e) { return $e->getMessage() === 'The order was invalid.'; });});
Assert that a given exception was not thrown, or that nothing was thrown at all.
Use json, getJson, postJson, putJson, patchJson, deleteJson, and optionsJson to send JSON requests with the corresponding HTTP verbs.
<?phptest('making an api request', function () { $response = $this->postJson('/api/user', ['name' => 'Sally']); $response ->assertStatus(201) ->assertJson([ 'created' => true, ]);});
Access JSON response data as array variables on the response.
expect($response['created'])->toBeTrue();
assertJson() verifies that the given array exists within the JSON response. Other properties may be present and the test will still pass as long as the specified fragment is found.
The etc() method tells Laravel that additional properties may exist on the JSON object. Without it, the test will fail if any property is present that you did not make an assertion against. This protects against accidentally leaking sensitive data in responses.
Use has() and missing() to assert attribute presence or absence.
Use actingAs() to send requests as an authenticated user.
<?phpuse App\Models\User;use Illuminate\Foundation\Testing\RefreshDatabase;uses(RefreshDatabase::class);test('authenticated users can view the dashboard', function () { $user = User::factory()->create(); $response = $this->actingAs($user)->get('/dashboard'); $response->assertStatus(200);});test('unauthenticated users are redirected to login', function () { $response = $this->get('/dashboard'); $response->assertRedirect('/login');});
To authenticate against a specific guard, pass its name as the second argument.
Use withSession() to seed session data before a request and assertSessionHas() to verify session values.
<?phptest('session value is set', function () { $response = $this->withSession(['locale' => 'en'])->get('/'); $response->assertSessionHas('locale', 'en');});
Use UploadedFile::fake() to generate dummy files and images. Combine it with Storage::fake() to test file upload flows without touching the real filesystem.
<?phpuse Illuminate\Http\UploadedFile;use Illuminate\Support\Facades\Storage;test('avatars can be uploaded', function () { Storage::fake('avatars'); $file = UploadedFile::fake()->image('avatar.jpg'); $response = $this->post('/avatar', [ 'avatar' => $file, ]); Storage::disk('avatars')->assertExists($file->hashName());});
Assert that a file does not exist with assertMissing().