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.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
Useget, post, put, patch, or delete to send requests in your tests. These methods return an Illuminate\Testing\TestResponse instance with many assertion helpers.
Customizing request headers
UsewithHeaders() to add custom headers to the request before it is sent.
Cookies
UsewithCookie() or withCookies() to set cookie values before making a request.
Session and authentication
UsewithSession() to seed the session with data before sending a request.
actingAs() to authenticate a user for the duration of the request. Combine it with model factories to create users on the fly.
actingAsGuest().
Debugging responses
Usedump, dumpHeaders, and dumpSession to inspect response contents without stopping execution.
dd, ddHeaders, ddBody, ddJson, or ddSession.
Exception handling
Use theExceptions facade to assert that your application throws specific exceptions.
Testing JSON APIs
Usejson, getJson, postJson, putJson, patchJson, deleteJson, and optionsJson to send JSON requests with the corresponding HTTP verbs.
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
UseassertExactJson() to verify that the response JSON exactly matches the given array.
Asserting on JSON paths
UseassertJsonPath() to verify a value at a specific dot-notation path in the JSON response.
assertJsonPath() for dynamic assertions.
Fluent JSON testing
Pass a closure toassertJson() to receive an AssertableJson instance and chain assertions fluently.
has() and missing() to assert attribute presence or absence.
hasAll() and missingAll() to check multiple attributes at once.
Asserting against JSON collections
Usehas() to assert the count of items and inspect the first item with first().
each() to apply the same assertions to every item in the collection.
Asserting JSON types
UsewhereType() and whereAllType() to assert that properties have the expected type.
| character to accept multiple types. The assertion passes if the value matches any of the specified types.
string, integer, double, boolean, array, null.
Testing authentication
UseactingAs() to send requests as an authenticated user.
User registration flow example
Test a registration endpoint end-to-end.Testing sessions
UsewithSession() to seed session data before a request and assertSessionHas() to verify session values.
Session assertion reference
Testing file uploads
UseUploadedFile::fake() to generate dummy files and images. Combine it with Storage::fake() to test file upload flows without touching the real filesystem.
assertMissing().
Customizing fake files
Specify dimensions and file size to test validation rules.Testing views
Render a view directly without making an HTTP request. Theview() 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.
withViewErrors().
Rendering Blade and components
Useblade() to evaluate a raw Blade template string.
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
Related pages
Testing
Learn the basics of writing tests in Laravel, including test structure, running tests, and common assertions.