Automated testing in Laravel with PHPUnit and Pest
Automated Testing in Laravel with PHPUnit and Pest
As software developers, we know that writing automated tests is crucial for ensuring the quality and reliability of our code. In this blog post, we will explore how to use PHPUnit and Pest to write automated tests for a Laravel application.
Why Automated Testing?
Automated testing helps us catch bugs and regressions early in the development cycle, reducing the risk of downstream problems. It also allows us to confidently make changes to our codebase, knowing that our tests will catch any issues that arise.
Getting Started with PHPUnit
PHPUnit is a popular testing framework for PHP that provides a lot of features out of the box. To get started with PHPUnit, you'll need to install it via Composer:
composer require --dev phpunit/phpunit
Once PHPUnit is installed, you can create a new test class by running the following command:
php artisan make:test MyTest
This will create a new test class in the `tests/Unit` directory. Let's take a look at an example test class:
use Tests\TestCase;
use Illuminate\Foundation\Testing\RefreshDatabase;
class MyTest extends TestCase
{
use RefreshDatabase;
public function testExample()
{
$this->assertTrue(true);
}
}
In this example, we're using the `RefreshDatabase` trait to ensure that our database is reset before each test. We're also using the `assertTrue` method to assert that the value `true` is indeed `true`.
Using Pest
Pest is a modern testing framework for PHP that provides a more concise and expressive way of writing tests. To get started with Pest, you'll need to install it via Composer:
composer require --dev pestphp/pest
Once Pest is installed, you can create a new test class by running the following command:
php artisan make:test MyTest
This will create a new test class in the `tests/Unit` directory. Let's take a look at an example test class:
use Tests\TestCase;
use Illuminate\Foundation\Testing\RefreshDatabase;
class MyTest extends TestCase
{
use RefreshDatabase;
public function testExample()
{
$this->assertTrue(true);
}
}
In this example, we're using the `RefreshDatabase` trait to ensure that our database is reset before each test. We're also using the `assertTrue` method to assert that the value `true` is indeed `true`.
Writing Tests for Laravel Controllers
To write tests for Laravel controllers, we can use the `Route::get` method to define a route that points to the controller action we want to test. Let's take a look at an example:
use Tests\TestCase;
use Illuminate\Foundation\Testing\RefreshDatabase;
class MyControllerTest extends TestCase
{
use RefreshDatabase;
public function testIndex()
{
$response = $this->get('/my-controller');
$response->assertStatus(200);
}
}
In this example, we're using the `get` method to send a GET request to the `/my-controller` route. We're then using the `assertStatus` method to assert that the response status code is 200.
Writing Tests for Laravel Models
To write tests for Laravel models, we can use the `factory` method to create a new instance of the model. Let's take a look at an example:
use Tests\TestCase;
use Illuminate\Foundation\Testing\RefreshDatabase;
class MyModelTest extends TestCase
{
use RefreshDatabase;
public function testExample()
{
$model = factory(MyModel::class)->create();
$this->assertTrue($model->exists);
}
}
In this example, we're using the `factory` method to create a new instance of the `MyModel` model. We're then using the `assertTrue` method to assert that the model exists.
Continuous Integration and Continuous Deployment (CI/CD)
CI/CD is the practice of automating the build, test, and deployment of software. To implement CI/CD in our Laravel application, we can use tools like Travis CI and CircleCI. Let's take a look at an example:
# .travis.yml
language: php
php:
- 7.4
install:
- composer install
script:
- vendor/bin/phpunit
deploy:
provider: script
script:
- php artisan deploy
In this example, we're using the `.travis.yml` file to define a Travis CI configuration. We're installing the `phpunit` package and running the tests using the `phpunit` command. We're also defining a deployment script that runs the `php artisan deploy` command.
Conclusion
In this blog post, we've explored how to use PHPUnit and Pest to write automated tests for a Laravel application. We've also covered how to write tests for Laravel controllers and models, and how to implement CI/CD using tools like Travis CI and CircleCI. By following these best practices, we can ensure that our code is reliable, maintainable, and scalable.