Introduction

Testing in Laravel is a fundamental part of the framework, emphasizing the importance of having a robust set of tests that ensure your application functions as expected before it goes into production. Laravel provides a rich set of tools and helpers to facilitate both unit and feature testing, making it easier to write and run tests efficiently.

Laravel is built with testing in mind. In fact, support for testing with Pest and PHPUnit is included out of the box and a phpunit.xml file is already set up for your application. The framework also ships with convenient helper methods that allow you to expressively test your applications.

By default, your application’s tests directory contains two directories: Feature and Unit. Unit tests are tests that focus on a very small, isolated portion of your code. In fact, most unit tests probably focus on a single method. Tests within your “Unit” test directory do not boot your Laravel application and therefore are unable to access your application’s database or other framework services.

Feature tests may test a larger portion of your code, including how several objects interact with each other or even a full HTTP request to a JSON endpoint. Generally, most of your tests should be feature tests. These types of tests provide the most confidence that your system as a whole is functioning as intended.

An ExampleTest.php file is provided in both the Feature and Unit test directories. After installing a new Laravel application, execute the vendor/bin/pest, vendor/bin/phpunit, or php artisan test commands to run your tests.

Environment

Creating Tests

To create a new test case, use the make:test Artisan command. By default, tests will be placed in the tests/Feature directory:

php artisan make:test UserTest

 

Running Tests

Running Tests in Parallel

Reporting Test Coverage

Profiling Tests

 

 

Leave A Comment