Testing is used to check whether your application is working correctly.

Types of Testing

  • Unit Testing
  • Feature Testing

Create a Test:

Unit Test:

php artisan make:test CalculatorTest --unit
tests/
 └── Unit/
      └── CalculatorTest.php

Feature Test:

php artisan make:test LoginTest
tests/
 └── Feature/
      └── LoginTest.php

Testing in Laravel is used to verify that the application works correctly. Laravel supports Unit Testing for individual methods and Feature Testing for complete application features like login, registration, and API endpoints. Tests are created using Artisan and run with php artisan test.

 

Scenario

Route::get('/hello', function () {
    return "Hello Laravel";
});

Create Future Test

php artisan make:test HelloTest
<?php

namespace Tests\Feature;

use Tests\TestCase;

class HelloTest extends TestCase
{
    public function test_hello_page()
    {
        $response = $this->get('/hello');

        $response->assertStatus(200);

        $response->assertSee('Hello Laravel');
    }
}

Run Test:

php artisan test