Test Driven Development (TDD) in application development in Laravel
I am going to talk to you about the TDD or Test Driven Development technique based on a test for the paginated listing and detail of a blog.
In this post, I will talk about TDD and an example of how this technique works and why it makes sense to use this type of techniques when developing.
What is TDD?
As an additional consideration, we talked about a technique called Test Driven Development (TDD), also known as test-driven development, which is a programming practice in which tests are written before the code is created functionality following the following key aspects:
- By defining the tests first, it allows you to specify each functionality before writing the actual code, it is like a kind of mental tree, but with code, in this way, the development process is guided.
- Clean and robust code: The goal is to create clean, robust and simple code. If the tests fail, the errors are corrected before moving forward.
Example to evaluate Test Driven Development
To give an example of the importance of testing in software development and of course, that it applies to development in Laravel.
The test of the paginated list will be similar to that carried out in the Rest API, but the evaluation of the response is not a JSON but a paginated list, specific methods are used for this purpose:
tests\Feature\Web\BlogTest.php
<?php
namespace Tests\Feature\Web;
use App\Http\Controllers\blog\BlogController;
use App\Models\Post;
use Illuminate\Foundation\Testing\DatabaseMigrations;
use Illuminate\Pagination\LengthAwarePaginator;
use Tests\TestCase;
class BlogTest extends TestCase
{
use DatabaseMigrations;
function test_index()
{
// $response = $this->get('/blog')
$this
->get(route('blog.index'))
->assertStatus(200)
->assertViewIs('blog.index')
->assertSee('Post List')
->assertViewHas('posts', Post::paginate(2));
$this->assertInstanceOf(LengthAwarePaginator::class,$response->viewData('posts'));
}
}
In this test, we will see several interesting aspects, for a change, we show that we can also use a named route:
->get(route('blog.index'))
With this assertion method, we check for the name of the view, along with its path:
->assertViewIs('blog.index')
With this method, we verify the data provided to the view and its name, which in this case is posts, which is the paginated list for the posts:
->assertViewHas('posts', Post::paginate(2));
With the following assertion method, we obtain the data of the view:
$response->viewData('posts')
And we verify that it is an instance of a class, by using:
Post::paginate(2)
We know it's from LengthAwarePaginator:
$this->assertInstanceOf(LengthAwarePaginator::class,$response->viewData('posts'));
This test, which is our first real test on the Laravel app that returns a view, means HTML content generated with blade and not something as simple or flat as a JSON and with this, we can see more specific assertion methods to ensure that The data has the expected format, as you can see, these tests also serve to specify where and how elements such as data, view and route should be composed, therefore, when specifying a clear structure, techniques such as TDD make sense. In short, when developing a new project, you first start with testing and it is the testing that specifies what should be implemented at the functional level.
- Andrés Cruz
This material is part of my complete course and book; You can purchase them from the books and/or courses section, Curso y Libro Laravel 11 con Tailwind Vue 3, introducción a Jetstream Livewire e Inerta desde cero - 2025.
Develop with Laravel, Django, Flask, CodeIgniter, HTML5, CSS3, MySQL, JavaScript, Vue, Android, iOS, Flutter