Laravel (5.7) Testing Getting Started
This tutorial is going to divided into three sections: an introduction section, an Environment section and a creating and running tests section, so sit tight as we get you started with testing in Laravel.
Introduction
The Laravel framework is built with testing in mind. In fact, the support for testing with PHPUnit has been included out of the box and a phpunit.xml file has already been set up for your application. The framework also ships with convenient helper methods that will allow you to expressively test your applications.
By default, the tests directory of your application contains two directories: The Feature and Unit directories. The Unit tests are tests that focus on a very small, isolated portion of your code. In fact, most unit tests will probably focus on a single method. Feature tests can test a larger portion of your code, this includes how several objects interact with each other or even the full HTTP request to a JSON endpoint.
An ExampleTest.php file has been provided in both the Feature and Unit test directories. After installing a new Laravel application, you can run phpunit on the command line to run your tests.
Environment
When you run tests via phpunit, Laravel automatically sets the configuration environment to testing because of the environment variables that are defined in the phpunit.xml file. Laravel will also automatically configure the session and cache to the array driver while testing, this means that no session or cache data is persisted while testing.
You are however free to define other testing environment configuration values as you deem necessary. The testing environment variables can be configured in the phpunit.xml file, but make sure to clear your configuration cache using the config:clear Artisan command before you run your tests!
Additionally, you can create a .env.testing file in the root of your project. This file will help you to override the .env file when you are running PHPUnit tests or when you are executing Artisan commands with the --env=testing option.
Creating & Running Tests
If you want to create a new test case, you can use the make:test Artisan command:
// This will create a test in the Feature directory...
```php artisan make:test UserTest```
//This will create a test in the Unit directory...
```php artisan make:test UserTest -unit
Once you have generated the test, you can define test methods as you normally would, using PHPUnit. If you want to run your tests, you should execute the phpunit command from your terminal:
<?php
namespace Tests\Unit;
use Tests\TestCase;
use Illuminate\Foundation\Testing\RefreshDatabase;
class ExampleTest extends TestCase
{
/**
* this is a basic test example.
*
* @return void
*/
public function testBasicTest()
{
$this->assertTrue(true);
}
}
Note: when you define your own setup method within a test class, you should be sure to call parent::setup().
Previous:
Laravel (5.7) Eloquent Serialization
Next:
Laravel (5.7) Console Tests
It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.
https://www.w3resource.com/laravel/testing-getting-started.php
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics