Skip to main content

Introduction

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.
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.

Making requests

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.
Each test should generally make only one request. Making multiple requests in a single test method can cause unexpected behavior.

Customizing request headers

Use withHeaders() to add custom headers to the request before it is sent.

Cookies

Use withCookie() or withCookies() to set cookie values before making a request.

Session and authentication

Use withSession() to seed the session with data before sending a request.
Use actingAs() to authenticate a user for the duration of the request. Combine it with model factories to create users on the fly.
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.
To send an unauthenticated request, use actingAsGuest().

Debugging responses

Use dump, dumpHeaders, and dumpSession to inspect response contents without stopping execution.
To dump and stop execution, use dd, ddHeaders, ddBody, ddJson, or ddSession.

Exception handling

Use the Exceptions facade to assert that your application throws specific exceptions.
Assert that a given exception was not thrown, or that nothing was thrown at all.
Disable exception handling for a request to let exceptions propagate to the test.
Assert that code within a closure throws a specific exception type.
Assert that no exception is thrown.

Testing JSON APIs

Use json, getJson, postJson, putJson, patchJson, deleteJson, and optionsJson to send JSON requests with the corresponding HTTP verbs.
Access JSON response data as array variables on the response.
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.

Asserting exact JSON matches

Use assertExactJson() to verify that the response JSON exactly matches the given array.

Asserting on JSON paths

Use assertJsonPath() to verify a value at a specific dot-notation path in the JSON response.
Pass a closure to assertJsonPath() for dynamic assertions.

Fluent JSON testing

Pass a closure to assertJson() to receive an AssertableJson instance and chain assertions fluently.
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 hasAll() and missingAll() to check multiple attributes at once.

Asserting against JSON collections

Use has() to assert the count of items and inspect the first item with first().
Use each() to apply the same assertions to every item in the collection.

Asserting JSON types

Use whereType() and whereAllType() to assert that properties have the expected type.
Use the | character to accept multiple types. The assertion passes if the value matches any of the specified types.
Supported types: string, integer, double, boolean, array, null.

Testing authentication

Use actingAs() to send requests as an authenticated user.
To authenticate against a specific guard, pass its name as the second argument.

User registration flow example

Test a registration endpoint end-to-end.

Testing sessions

Use withSession() to seed session data before a request and assertSessionHas() to verify session values.

Session assertion reference

Testing file uploads

Use UploadedFile::fake() to generate dummy files and images. Combine it with Storage::fake() to test file upload flows without touching the real filesystem.
Assert that a file does not exist with assertMissing().

Customizing fake files

Specify dimensions and file size to test validation rules.

Testing views

Render a view directly without making an HTTP request. The view() method returns an Illuminate\Testing\TestView instance.
TestView provides the following assertion methods: Cast the TestView to a string to access the raw rendered HTML.
Inject errors into the view’s error bag with withViewErrors().

Rendering Blade and components

Use blade() to evaluate a raw Blade template string.
Use component() to render a Blade component class. Returns an Illuminate\Testing\TestComponent instance.

Response assertion reference

HTTP status

Redirects

Content

JSON

Headers and cookies

Views

Validation

Testing

Learn the basics of writing tests in Laravel, including test structure, running tests, and common assertions.
Last modified on April 25, 2026