Oh Dear is the all-in-one monitoring tool for your entire website. We monitor uptime, SSL certificates, broken links, scheduled tasks and more. You'll get a notifications for us when something's wrong. All that paired with a developer friendly API and kick-ass documentation. O, and you'll also be able to create a public status page under a minute. Start monitoring using our free trial now.

How to test all routes in your Laravel app by writing just a single Pest test

Original – by Freek Van der Herten – 4 minute read

We’ve released a new package called spatie/pest-plugin-route-testing. This package can test all common routes in your Laravel app by writing a single test.

In this blog post, I’d like to tell you all about it.

Why this package was created

When people start out writing tests for their Laravel apps, I usually give the advice to test your home page. The homepage usually is a very simple URL to test; it’s just a simple GET request. Here’s a video that explains how to test the homepage taken from our Testing Laravel course. Here’s how such a test could like:

it('can render the homepage', function() {
   $this
      ->get('/')
      ->assertSuccessful();
});

Now, in real-world applications, there are typically more simple pages like the homepage, where you could write a simple test to make sure that it works. When adding simple pages to your application, it’s easy to forget to write a test for them.

That’s why we created our new spatie/pest-plugin-route-testing package. It allows you to write a single test to make sure all simple GET requests like pages in your app work.

Using spatie/pest-plugin-route-testing

use function Spatie\RouteTesting\routeTesting;

routeTesting('all GET routes')->assertSuccessful();

This will test all GET routes in your application and ensure they return a 200 HTTP status code. Here's what the output looks like when you run this test in a small app.

Instead of assertSuccessful(), you can use any assertion that is available in Laravel's TestResponse class, such as assertRedirect(), assertNotFound(), assertForbidden(), etc.

You can also test specific routes:

use function Spatie\RouteTesting\routeTesting;

routeTesting('all blog routes')
    ->include('blog*')
    ->assertSuccessful();

If there are routes that have route model bindings, the package will skip the test for those routes. Let's assume you have a route defined as user/{user}. Here's what the output looks like when you run the test.

If you want to test a route with a route model binding, you can provide the model using the bind method.

use function Spatie\RouteTesting\routeTesting;
use App\Models\User;

routeTesting('all blog routes')
    ->bind('user', User::factory()->create())
    ->assertSuccessful();

When you run the test now, the package will use the provided model to test the route.

If you want to exclude routes from the test, you can use the exclude method. Here's an example that tests all routes except the ones that start with admin.

use function Spatie\RouteTesting\routeTesting;

routeTesting('all blog routes')
    ->exclude('admin*')
    ->assertSuccessful();

You can use the setUp method to execute code before the route test is run. Here's an example where we log in a user before running the test.

use function Spatie\RouteTesting\routeTesting;

routeTesting('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();

In closing

Using spatie/pest-plugin-route-testing, it’s easy to test all simple pages of your Laravel app. New pages will automatically be tested too.

My colleague Niels came up with the original idea for this package, and Alex did a big refactor on the internals.

This isn’t the first package our team has created. You can see everything we do in open-source on the open-source pages on our company website.

Stay up to date with all things Laravel, PHP, and JavaScript.

You can follow me on these platforms:

On all these platforms, regularly share programming tips, and what I myself have learned in ongoing projects.

Every month I send out a newsletter containing lots of interesting stuff for the modern PHP developer.

Expect quick tips & tricks, interesting tutorials, opinions and packages. Because I work with Laravel every day there is an emphasis on that framework.

Rest assured that I will only use your email address to send you the newsletter and will not use it for any other purposes.

Comments

Gary Mark avatar

Thank you for your article, the information is really useful. You can refer https://freek.dev/2714-handling-email-testing-in-laravel-with-raycookie clickerto know more

Fesicop12 avatar

The virtual abode of Papa Louie welcomes you. Here you can play all the top Papa Louie games online for free. So, how does a Papa Louie game work? Exciting challenge, questionable business strategies, and delicious cuisine! Cooking and restaurant management are significant themes in most of papa's games. Exciting and demanding, working in Papa's kitchen is a great experience.

selpinka avatar

The game is an excellent geometry dash tool for teaching perseverance. With many levels requiring multiple attempts, players learn to persist despite challenges.

apnehindi avatar

To test all routes in a Laravel app with a single Pest test, loop through the routes using Route::getRoutes() and assert the response for each GET route using assertOk(). For Apne Hindi TV speed testing, use tools like Google PageSpeed Insights or GTmetrix to analyse and optimise performance.

Chaylen Jordan avatar

The more you play, the more you get hooked, geometry dash is a masterpiece of platform gaming!

Liwovosa avatar

Hiya, I’m really glad I have found this information. Nowadays bloggers publish only about gossips and net and this is actually annoying. A good website with interesting content, that’s what I need. Thanks for keeping this web-site, I will be visiting it. Do you do newsletters? Can’t find it. block blast online

Liwovosa avatar

That's the reason concentrate on you have to particular footwork ahead of when writing. Is going to be feasible in order to much more appealing article in this way. Popcorn game

we become what we behold avatar
we become what we behold

Wow those are great ideas how did you get them henry stickman unbelievable

ella john avatar

I am really thankful to you for everything you have done for me, both in terms of my personal and professional development. Without your advice, I would not have been able to make progress in either of these areas. play space waves game

Master123 avatar

That's great content and reply. You can also get help from kids motorbike leather suit and oversized leather jacket

Allen Go avatar

I'm truly grateful for everything you've done for me regarding my personal and professional development. Thanks to your insightful articles, I was inspired to create this mini-game website, featuring the dinogame. Without your guidance, I wouldn't have made such progress. Thank you for your invaluable support!

devstimes avatar

I'm truly grateful for everything you've done for me regarding my personal and professional development. Thanks to your insightful articles, I was inspired to create this mini-game website, flappy dunk, featuring exciting games like FNAF Shooter. Without your guidance, I wouldn't have made such progress. Thank you for your invaluable support!

Cyre avatar

what if some routes need specific data or permissions? It’d be nice if it handled more complex scenarios out of the box. but I’m not sure if it covers enough. geometry dash spam

yee avatar

Play Sprunki Retake Game Free Online at https://www.sprunkiretake.co/, Unleash your creativity with Sprunki Retake! Dive into enhanced beats, visuals, and unique styles!

yee avatar

Play Sprunki Retake Game Free Online at https://www.sprunkiretake.co/, Unleash your creativity with Sprunki Retake! Dive into enhanced beats, visuals, and unique styles!

Comments powered by Laravel Comments
Want to join the conversation? Log in or create an account to post a comment.