Updated 4 October 2024
Testing is essential for building reliable applications, ensuring that your code behaves as expected. Laravel uses PHPUnit for testing, but Pest offers a modern, easy-to-read alternative.
In this guide, we’ll demonstrate how to integrate and write effective test cases in UnoPim.
Before diving into writing Pest tests, you need to set up Pest. Here’s how you can do it:
pestphp/pest
version ^2.6 To ensure your tests are recognized, update your composer.json
to include:
"autoload-dev": {
"psr-4": {
"Webkul\\Admin\\Tests\\": "packages/Webkul/Admin/tests",
}
}
Before running your tests, it’s crucial to configure a separate environment for testing. This ensures that your tests do not interfere with your development or production databases.
.env.testing
File by creating a copy of .env.example
file.DB_DATABASE=your_testing_database_name
To ensure that all test suites are recognized, you need to update your phpunit.xml file. Add the following section:
<testsuites>
<testsuite name="User Test">
<directory suffix="Test.php">.
/packages/Webkul/Admin/tests/Unit
</directory>
</testsuite>
</testsuites>
This configuration allows PHPUnit to locate and run tests from various packages, ensuring comprehensive coverage.
In addition to the configuration above, specify custom test cases for each package by adding the following lines in your tests/Pest.php:
The uses()
function binds specific PHPUnit test case classes to corresponding test directories. This allows you to define common functionality and setup for your tests within those classes.
uses(Webkul\Admin\tests\AdminTestCase::class)
->in('../packages/Webkul/Admin/tests');
Your AdminTestCase
class should look like this:
<?php
namespace Webkul\Admin\Tests;
use Tests\TestCase;
use Webkul\User\Tests\Concerns\UserAssertions;
class AdminTestCase extends TestCase
{
use UserAssertions;
}
Unit tests are focused on individual components or methods. Let’s start by writing a unit test for a simple scenario in the Unopim project.
Suppose Unopim has a User model with a getFullName method that combines the first name and last name. Create a new test file at packages/Webkul/Admin/tests/Unit/UserTest.php
. Open this file and add:
use App\Models\User;
it('should get full name', function () {
$user = User::where('id', 1)->first();
expect($user->getFullName())->toBe('John Doe');
});
In Pest, tests are written in a more readable syntax, making them easier to understand.For more information on available expectation functions, visit the Pest Expectations Documentation.
Execute the tests with:
./vendor/bin/pest
You should see the tests results in your terminal as in the above image.
Feature tests cover broader application aspects like HTTP requests. Let’s write a feature test for an API endpoint in Unopim.
Create a new test file at packages/Webkul/Admin/tests/Feature/UserTest.php
Add the below code in your test file.
use App\Models\User;
it('should fetch a user by id', function () {
$user = User::factory()->create();
$response = $this->json("/api/users/{$user->id}");
$response->assertStatus(200)
->assertJson([
'id' => $user->id,
'first_name' => $user->first_name,
'last_name' => $user->last_name,
]);
});
Execute the test using: ./vendor/bin/pest
For comprehensive guidance on creating tests, refer to the Pest Writing Tests Documentation. Additionally, you can explore the PHPUnit Assertions Documentation for a variety of assertion functions that you can use in your test cases.
Integrating Pest into your Laravel project can streamline and enhance your testing process. By following these guidelines, you’ll be able to create robust test cases in UnoPim. If you have any questions or need further assistance with Pest or Unopim testing, feel free to reach out or leave a comment below. Happy testing with Pest!
We hope you find this guide helpful! If you encounter any issues, please feel free to submit a ticket at our Support Portal.
If you have more details or questions, you can reply to the received confirmation email.
Back to Home