Laravel Testing: Starting with Laravel feature Testing

12 Dec 2018 . Laravel . 404 views

Although, there is a lot of controversy for testing in development, however, I would prefer to do follow testing. The advantage of testing laravel application is better than without testing. So, I will write down some continuous post on it. So, today, I will show you how to create a new feature test on laravel application.

There are two types of testing in Laravel by default, unit, and feature. Today I will show you how to create a new feature testing in Laravel.

Firstly, you need to create a new feature test by php artisan command.

php artisan make:test TheNameOfYourTestFile

The above command will create a testing file called TheNameOfYourTestFile in the Feature directory inside the tests folder.

The file will look like that-

<?php

namespace Tests\Feature;

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

class TheNameOfYourTestFile extends TestCase
{
    /**
     * A basic test example.
     *
     * @return void
     */
    public function testExample()
    {
        $this->assertTrue(true);
    }
}

Now, you can define your test function. There is a default method inside the class called testExample that you can overwrite. Let's do that-

/**
     * A basic test example.
     *
     * @return void
     */
    public function testAVisitorCanSignUp()
    {
        $this->assertTrue(true);
    }

After saving the file, now let's run testing. To run, you need to open the terminal and redirect to the project directory and then use the following command.

phpunit

You might be encountered an error that phpunit command not found: phpunit. So, you need to show where is php unit is located. Let do it once again-

vendor/bin/phpunit

It should show you the error or success message.

PHP Test Result

Cool. You have run your first PHP Test. Let's dig into the next location.