Folder structure
In this article, we will explore how to organize tests effectively, using the example of a feature that allows users to write and publish a blog post. Consider an API endpoint that allows authenticated users to add a new post. There are various way to sturcture your tests, for instance you could create a Feature/PostTest class and write all the test realted to a feature about creating a post in this class.
Benefits
Drawbacks of this structure:
I personally prefer this approach when working alone or in a small team. However, in larger codebases, it can be challenging to group and identify tests for each feature effectively, making this structure harder to maintain.
Another approach of organizaing folder strucure is to create a Test class that mirrors the directory of the corrosponding code files but resides within the tests folder, for example, if a controller is located at src/application/controllers/BlogController.php the test for this controller will be written in tests/Feature/application/controllers/BlogControllerTest.php. In this setup, the Feature prefix refers to complete end-to-end tests.
Benefits
Test Setup
To create a new Laravel application, use the command laravel new laravel-tdd. During the setup, you'll be prompted to select a testing framework. For simplicity, I chose PHPUnit as the testing framework and sqlite as the database. Laravel includes a basic test setup by default, and you can run your tests using php artisan test. By default, it runs and passes two tests.
Database Setup
Consider a feature, where user should able to write a blog post and publish it. Consider a blog contains only title and body for now for simplicity. To implement this feature we need database table, Model and Controller, so Let's quickly create a database model, migrations, factory and controller with this command php artisan make:model Blog -mfcr. The following files are created below:
Let's add title and body into our migration.
In this article, we will explore how to organize tests effectively, using the example of a feature that allows users to write and publish a blog post. Consider an API endpoint that allows authenticated users to add a new post. There are various way to sturcture your tests, for instance you could create a Feature/PostTest class and write all the test realted to a feature about creating a post in this class.
Benefits
- Each feature has its own dedicated test class.
- All tests related to a specific feature are grouped within a single class.
- Maintains a flat folder structure without nested levels, simplifying organization.
- Ideal for small codebases and teams with fewer members.
Drawbacks of this structure:
- In larger codebases, it becomes challenging to locate specific test cases related to the code.
- All the test cases related to one feature will be in single file
- Difiiculties to mange in larger team or larger code base.
I personally prefer this approach when working alone or in a small team. However, in larger codebases, it can be challenging to group and identify tests for each feature effectively, making this structure harder to maintain.
Another approach of organizaing folder strucure is to create a Test class that mirrors the directory of the corrosponding code files but resides within the tests folder, for example, if a controller is located at src/application/controllers/BlogController.php the test for this controller will be written in tests/Feature/application/controllers/BlogControllerTest.php. In this setup, the Feature prefix refers to complete end-to-end tests.
Benefits
- Simplifies locating tests for a specific piece of code and vice versa.
- Enhances organization, making tests easily discoverable, especially in larger teams.
Test Setup
To create a new Laravel application, use the command laravel new laravel-tdd. During the setup, you'll be prompted to select a testing framework. For simplicity, I chose PHPUnit as the testing framework and sqlite as the database. Laravel includes a basic test setup by default, and you can run your tests using php artisan test. By default, it runs and passes two tests.
Database Setup
Consider a feature, where user should able to write a blog post and publish it. Consider a blog contains only title and body for now for simplicity. To implement this feature we need database table, Model and Controller, so Let's quickly create a database model, migrations, factory and controller with this command php artisan make:model Blog -mfcr. The following files are created below:
- app/Http/Controllers/BlogController.php
- app/Models/Blog.php
- database/migrations/2025_01_22_195944_create_blogs_table.php
- database/factories/BlogFactory.php
Let's add title and body into our migration.