Automated Testing with PHPUnit and Laravel Dusk
Automated testing is a critical part of modern web development, ensuring the reliability and stability of your application. In this guide, we will explore how to use PHPUnit and Laravel Dusk for effective testing in a Laravel project.
What is PHPUnit?
PHPUnit is a testing framework for PHP that allows developers to write unit tests for their code. It comes pre-installed with Laravel and is perfect for testing individual components and logic.
Setting Up PHPUnit in Laravel
Laravel includes PHPUnit by default. Ensure it's installed via Composer:
composer require --dev phpunit/phpunit
Writing a Basic Test
Create a test file using the artisan
command:
php artisan make:test ExampleTest
A basic test example (tests/Feature/ExampleTest.php
):
<?php
namespace Tests\Feature;
use Tests\TestCase;
class ExampleTest extends TestCase
{
public function test_homepage_loads_correctly()
{
$response = $this->get('/');
$response->assertStatus(200);
}
public function test_redirect_from_homepage()
{
$response = $this->get('/');
$response->assertRedirect('/dashboard');
}
public function test_api_endpoint_returns_data()
{
$response = $this->getJson('/api/users');
$response->assertStatus(200)
->assertJsonStructure([
'*' => ['id', 'name', 'email']
]);
}
}
Run the tests:
php artisan test
What is Laravel Dusk?
Laravel Dusk is a browser automation testing tool for Laravel, built on top of Selenium. It allows developers to test web interfaces and user interactions directly in a browser.
Installing Laravel Dusk
composer require --dev laravel/dusk
php artisan dusk:install
Setting Up Dusk Environment
Ensure the .env
file has the proper settings for Dusk:
APP_ENV=testing
APP_DEBUG=true
Writing Basic Dusk Tests
Create a new Dusk test:
php artisan dusk:make LoginTest
Example test (tests/Browser/LoginTest.php
):
<?php
namespace Tests\Browser;
use Laravel\Dusk\Browser;
use Tests\DuskTestCase;
class LoginTest extends DuskTestCase
{
public function test_user_can_login()
{
$this->browse(function (Browser $browser) {
$browser->visit('/login')
->type('email', '[email protected]')
->type('password', 'password')
->press('Login')
->assertPathIs('/dashboard');
});
}
public function test_registration_form()
{
$this->browse(function (Browser $browser) {
$browser->visit('/register')
->type('name', 'Test User')
->type('email', '[email protected]')
->type('password', 'password123')
->type('password_confirmation', 'password123')
->press('Register')
->assertPathIs('/dashboard');
});
}
}
Running Dusk Tests
php artisan dusk
Best Practices for Automated Testing in Laravel
- Write tests for critical paths: Focus on key functionalities first.
- Keep tests isolated: Avoid dependencies between tests.
- Use Factories: Simplify test data creation using Laravel factories.
- Continuous Integration: Integrate PHPUnit and Dusk tests in your CI/CD pipeline.
Conclusion
Using PHPUnit and Laravel Dusk together provides a complete testing strategy for Laravel applications. PHPUnit is ideal for unit and feature testing, while Dusk excels in end-to-end and browser interaction testing. Implement both to ensure your application remains stable and bug-free.