PLATFORM
  • Tails

    Create websites with TailwindCSS

  • Wave

    Start building the next great SAAS

  • Pines

    Alpine & Tailwind UI Library

  • Auth

    Plug'n Play Authentication for Laravel

  • Designer comingsoon

    Create website designs with AI

  • DevBlog comingsoon

    Blog platform for developers

  • Static

    Build a simple static website

  • SaaS Adventure

    21-day program to build a SAAS

Written By
Views

Simple Laravel Route Testing

Simple Laravel Route Testing

Laravel makes it super easy to add tests to your application. There may be times where you want to create a quick PHPUnit test to confirm your application URL's return a 200 request.

I'm not suggesting that you only include tests that check all your route applications; however, this will be better than not including any tests at all.

If you wish to add these tests to your Laravel application you can start off by deleting the PHPUnit file located at tests/Features/ExampleTest.php and then run the following command to create a new test:

php artisan make:test RouteTest.php

This will create a new file located at tests/Features/RouteTest.php, inside of this file you can add the following:

<?php

namespace Tests\Feature;

use Tests\TestCase;
use Illuminate\Foundation\Testing\RefreshDatabase;

class RouteTest extends TestCase
{
    /**
     * A basic test example.
     *
     * @return void
     */
    public function testRoutes()
    {
        $appURL = env('APP_URL');

        $urls = [
            '/',
            '/url2',
            '/url3' 
        ];

        echo  PHP_EOL;

        foreach ($urls as $url) {
            $response = $this->get($url);
            if((int)$response->status() !== 200){
                echo  $appURL . $url . ' (FAILED) did not return a 200.';
                $this->assertTrue(false);
            } else {
                echo $appURL . $url . ' (success ?)';
                $this->assertTrue(true);
            }
            echo  PHP_EOL;
        }

    }
}

Make sure to enter your application URL's in the $urls array above. Lastly, run the following in your command prompt:

vendor/bin/phpunit

And this will run PHP unit and check that all the application routes inside of the $urls array return a status code of 200, you will see an output that looks similar to the following:

Simple Laravel Route Tests

And just like that you have some Simple Laravel Route tests in your application ;)

Comments (0)

loading comments