(1) How To Use Feature Test in Your Laravel Project ?
To testing is most important side of development for advanced projects. When your project is very detailed, it is difficult to track errors, problems and mistakes. At this point, we need to testing the project at risky side or every point of apis, methods.
Laravel use unit tests to make easier to testing. We have some default setting file (phpunit.xml) at main path of Laravel. You can use this file to change settings for your applications.
We have two option to test our laravel project, first one is “Feature Test” and the second one is “Unit Test”. In this article, we will only discuss for Feature Test.
What is Feature Test ?
Feature test is used to test major portion of your projects. You can directly make a test request to all your endpoints, get response and be sure you get expected response content from your endpoints.
An Example Of Feature Test in Laravel
Let’s create an endpoint at api.php
Route::get('testing', function () {
return response()->json([
'message' => 'Successful',
'status' => true
]);
})->name('test.endpoint');
We also define a name (test.endpoint) for our endpoint, then we can call directly this endpoint at test. You can use it also at your projects, if you still does not. It will be very useful for your project.
Let’s create also a new test file with php artisan.
php artisan make:test TrainingTest
You will see the test file, under the tests/Feature path and you will see a default content of your test class.
class TrainingTest extends TestCase
{
/**
* A basic feature test example.
*
* @return void
*/
public function test_example()
{
$response = $this->get('/');
$response->assertStatus(200);
}
}
Now, we can test our basic endpoint. In our example endpoint should response 200 and two parameter. We can also directly check, which parameters and values has our response. We already now, what will be responded by api for success request.
/**
* In this case, we request to our test endpoint.
* We expect that, a message and status parameter.
*
* @return void
*/
public function test_example(): void
{
$response = $this->get(route('test.endpoint'));
// Response should be 200
$response->assertStatus(200);
// Response exists a message variable and content of that.
$response->assertJsonPath('message', 'Successful');
// Response exists a boolean status parameter and should be true.
$response->assertJsonPath('status', true);
}
That’s all !
We tested our endpoint. It is really useful, image you have a really advenced project like e-commerce. Your customers use payment, order or other important services. We must be very careful at this process because there is really sensitive processes for users. If we use feature test, we can directly catch the errors.
How To Run Tests ?
You prepared your tests and want to run them now. We can use terminal to run them with php artisan.
php artisan test
You will also see a successfull result at your terminal.
Also see, what happens when response has no expected content.
Let’s imagine, something is wrong at our code for other scenerios and our api responded wrong value. Your test will be failed when you run it. We expect “Successful” value but real response is “Successfull”. Let’se see what our test will say us.
Route::get('testing', function () {
return response()->json([
'message' => 'Successfull',
'status' => true
]);
})->name('test.endpoint');
Thank you for your time !
Hope the content is useful for you !
Resources :
Enver ŞANLI
Fullstack Web Developer