Pest Route Testing Plugin for Laravel Applications
Last updated on by Paul Redmond
The pest-plugin-route-testing for Pest helps you ensure your routes are okay in Laravel. This package has useful methods to filter down which routes to run, bind users to test routes, and more. The simplest example is testing all GET
routes, which you can do with one method call:
use function Spatie\RouteTesting\routeTesting; routeTesting('all GET routes') ->assertSuccessful();
All your application routes have high-level validation that returns a 200
status code. That's pretty neat!
Route Testing Plugin Features
- Test specific routes with the
include()
method - Exclude routes from your test with the
exclude()
method - Route Model Binding support
- Run custom code before route testing
- Ignore skipped tests in the test result output
Here are a few more examples from the project's readme, illustrating how to use this plugin:
use function Spatie\RouteTesting\routeTesting; // Filter routes to includerouteTesting('all blog routes') ->include('blog*', 'post*') ->assertSuccessful(); // Exclude some routesrouteTesting('all blog routes') ->exclude('admin*') ->assertSuccessful(); // Bind a user to test all routesuse App\Models\User; routeTesting('all blog routes') ->bind('user', User::factory()->create()) ->assertSuccessful(); // Run some code beforerouteTesting('all admin routes') ->setUp(function () { $user = User::factory()->create(); $this->actingAs($user); // optionally, you could also bind the model $this->bind('user', $user); }) ->include('admin*') ->assertSuccessful();
You can get started with this package on GitHub at spatie/pest-plugin-route-testing. To install it in your project, add it as a Composer dependency:
composer require spatie/pest-plugin-route-testing