Laravel Unit Test Cases with Data Provider – DevstringxDeepak Sharma
Laravel is an open-source PHP framework, Which is widely used for web application development using PHP. Laravel provides the reusability of the code. And well-structured formatting of the code and files. It has a rich collection of libraries for different tasks.
It provides pre-installed libraries that are useful for most web applications. Due to the highly optimized code, it increases the loading speed of the application. It provides a large collection of features than any other PHP framework
Here we will learn about Unit Test Case in laravel
Create Project and Setup Project
Composer create-project Laravel/Laravel test project
After the project was created successfully. You need to change the database configuration in .env
DB_CONNECTION=mysql DB_HOST=127.0.0.1 DB_PORT=3306 DB_DATABASE=laravel DB_USERNAME=root DB_PASSWORD=
Create Test API
Create controller :
PHP artisan make:controller UserController
Write the below code in the UserController.php
And paste the below code in routes\api.PHP
Now check if the API is working. Run below command
PHP artisan serve
Read Also:- How to Install The Telescope In Laravel
Create Your First Unit Test Case
Run the below common in terminal: PHP artisan make:test UserTest –unit Past the blow code in the AddTwoNumberTest file. This file contains the data providers, payload, and test function. Pls, read them carefully.
Now your test cases are ready to run. Use the below command to execute the test cases
Read Also:- How to Install CodeIgniter4?
PHP Artisan Test
Congratulations you have successfully run the Unit test cases with dataProviders in laravel