- What is Laravel?
- Upgrade Laravel To 8.X
- Role Management
- Newsletter Package
- Laravel Vs WordPress CMS
- New Laravel 6.X
- How To Make API In Laravel
- Laravel RSS/XML Feed
- How To Make Theme In Laravel?
- Laravel Project Testing Using PHPunit
- Laravel Package
- Make Contact Form In Laravel
- Laravel Homestead
- Laravel Cron Job
- What is Composer
- How to create an Admin-Panel in Laravel
- Admin Panel in Laravel
- Installation of Laravel in Windows
- Laravel Development
- Benefits of Laravel
- Laravel Notification
- How to make a Blog in Laravel
- Laravel Authentication
Testing In Laravel
Laravel is built with testing in mind. Basically, Laravel will give you PHPUnit as a testing tool out of the box. By default, the phpunit.xml file is already set up for your application. In Laravel project, under the “tests” directories contains “Feature” and “Unit” directory. Where Feature tests focus on larger portion of your code while Unit tests focus on a very small, isolated portion of your code.
Before running the test in your project, you should have cleared the cache or session, means that no session or cache data will be persisted while testing. For clearing the cache or configuration, you can use the artisan command “php artisan cache:clear” or “php artisan config:clear”.
Now, in this article we’ll go through Creating & Running Tests in Laravel.
1. Install Laravel Project
Enter the composer command to install Laravel: composer-project –prefer-dist laravel/laravel LaravelTesting.
2. Creating New Test
2.1. To create a new test case, use the “make:test” artisan command:
Create a test in the Feature directory:
• php artisan make:test NameTest
Create a test in the Unit directory:
• php artisan make:test NameTest –unit
Create a new unit test: php artisan make:test UserModelTest –unit
After running the command, file will be generated under “tests/Unit/” directory. File will be look like this:
3. Running Test
After, test has been generated, go to your terminal and run your tests, execute the “./vendor/bin/phpunit” command from your terminal. This will test your 3 files, one is for “Feature/ExampleTest.php” file and two file for “Unit/ExampleTest.php” and “Unit/UserModelTest.php” files.
After execute the command, tests will look like this.
Now suppose, we have created one another new unit test file called NewFileTest:
Files will be generated under “tests” directory:
After generated the test file, go to your terminal and run the phpunit command: “./vendor/bin/phpunit”. This will check four files such as “Feature/ExampleTest.php”,“Unit/ExampleTest.php”,“Unit/UserModelTest.php” and “Unit/NewFileTest.php”.
Now, Let’s change the assertion in “tests/Unit/NewFileTest.php” file like this:
After changed and save it, go to your terminal and enter the “./vendor/bin/phpunit” command and you’ll get a failure assertion like this:
There are following PHPUnit test method or assertions and you can use these methods as per your test requirements. To learn about these assertions you can visit this link:
Some assertions are like this: