So in this article I’m going to explain how to test every route in a laravel application. I usually name all the routes in my application, so essentially, every route in my application has some sort of sanity test.
Here is a 1 minute video of me writing all the tests for my application with this script.
The TLDR; gist of this article is that I can run a command
And that command will generate some testing code for me based on routes that are missing. Something like this:
This essentially means two things.
- I don’t have to keep up with all routes I have and have not tested.
- It also creates a boilerplate for me to get started testing.
You can find the
missing-routes-test script HERE.
Since most applications have users, let’s create a RESTful resource to manage our users in our application. So let’s add a resourceful route to our
Next we need a resource controller, so let’s run the command to create it for us.
$ php artisan make:controller UsersController --resource
Next we want to test a route in Laravel, I can create it using
$ php artisan make:test UsersTest
That gets us some code generated out that looks something like this
class UsersTest extends TestCase
At this point, I want to test a route. Therefore, I add a method and use Laravel’s awesome testing methods to call the /users url.
Now when we run phpunit we get that green that everyone likes.
Granted, our test is really simple. It only checks for a response status of 200. If we navigate to this page it is blank because our
UsersController::index() is empty.
So are we done? No. We’ve only tested 1 route so far. Laravel ships with a nice artisan command to view all our routes.
$ php artisan route:list
For our basic users management application this spits out the following
We could manually go through this list and try to test at our own pace. But this method is prone to human errors and it’d be better to programmatically scan for the routes we have not yet tested.
Thus I run the following command to see which tests are missing from our users app.
This outputs the following
Now when I run
phpunit again, I at least have a rough idea of how many routes we have yet to test.
Right now I’m using annotations to mark a named route as tested. I decided for this approach because it seems easy. Another approach that might work is to run phpunit and keep track of which routes were called during the entire process. After that was finished, we could compare our list of routes to the entire list using
Route::getRoutes(). I opted for the annotation route just because I wanted something quick and easy.
Again you can find the script HERE.
If you find yourself using this or have some sort of improvements or even a better way for testing all the routes in your application, please leave me a comment. Thanks!