A Laravel application’s codebase has to be clean to achieve quality in the result. There are several methods to ensure this, however, we are going to study one of the most important ones; Test coverage. A metric used to make sure that the application runs smoothly under the test suite. It ensures that every functionality of a Laravel based app has been tested.
Test cases are an important thing when you develop APIs for your app. In this blog, we are going to learn how test case coverage helps to visualize the actual line of codes you have covered in your test cases. So, What are you waiting for let’s reduce the likelihood of bugs and errors from running your users’ app experience shall we?
Install Laravel Project
First start with the basics of installing process which ensures smooth operation of every file and functionality.
To Install Fresh Laravel Project Write the Below Command In Your CMD/Terminal :
composer create-project laravel/laravel CoverageDemo
Install Php XDebug
To Install Php Xdebug Write Below Two Commands In Your CMD/Terminal :
brew install autoconf
pecl install xdebug
After successfully installing we will configure the xdebug.so file in the php.ini file. Now we have successfully installed Xdebug in the system. It will download the xdebug.so file in the below path:
Note: The php8.1.13 shown in the image above is my system’s PHP version. You need to change the path based on your system’s current PHP version.
Php Xdebug Configuration
Open the php.ini file located below path :
Update the below xdebug.so file path in zend_extension & add xdebug.mode=coverage
You can add multiple Xdebug comma-separated modes. Here I have added develop, debug & coverage. The coverage is required for us now
Phpunit.xml Configuration
After successfully setting up Xdebug we are going to create a .env.testing file in the project’s root folder to run our test cases.
In this file, we are configuring the testing database details & you need to create your_project_db_test database in phpmyadmin.
After that, we are adding some coverage details in the phpunit.xml file as below to cover API controllers & API request files.
In this file, we also change
- DB_CONNECTION to MySQL & DB_DATABASE to your_project_db_test
- We can also define <include> files that we need for the coverage reports.
- Also <exclude> files that ignore the files from the coverage.
- Both include & exclude files covered with <coverage> as displayed below.
Coverage Reports
After that, we will execute the below command to migrate & seed into a new test database.
php artisan migrate: fresh –seed –env=testing
To view the coverage files percentage we are going to execute the below command
php artisan test –coverage
Here is the output of controllers & requests for APIs. You can see that 97.8 % of the code is covered through the test case. I have left some remaining APIs for testing in the UserController.php & VoucherController.php file so that it will display 68.9% & 91.3% code coverage.
I have left some remaining APIs for testing in the UserController.php & VoucherController.php file so that it will display 68.9% & 91.3% code coverage.
- We can also visualize this file using html file report.
To generate a test cases coverage report execute the below command.
php artisan test –coverage-html reports/
It will create a reports folder in your root directory of the project with some html files. We can see the dashboard.html file & index.html file are there in the reports folder.
Here we can see I have not written referral DeepLink() test cases. That’s why the coverage report is showing file content in RED color. Other lines that are covered in test cases are shown in GREEN color. So after that, you can cover the lines of code in your test that are remaining.
Development ServicesGet Expert Assistance
Conclusion
In conclusion, ensuring comprehensive test coverage is not just a technical necessity but a strategic investment in the long-term success of Laravel projects. It empowers teams to build with confidence, secure in the knowledge that their applications can withstand the demands of real-world usage. Whether you’re an individual developer or part of a Laravel development company, embracing these testing practices can elevate the standard of your Laravel applications, delivering exceptional value to users and stakeholders.