Laravel for Beginners: PHPUnit and PEST Tests

Arlind Musliu Portrait
Arlind Musliu

January 12, 2024 · 3 min read · 71 views

Laravel Blogpost Image

Testing Your Laravel Application

Testing is a crucial step in ensuring that your application works as expected and remains stable over time. Laravel, being a framework that embraces testing, provides excellent support for writing tests with PHPUnit, and has also seen growing support for PEST, a testing framework with a focus on simplicity and elegance. In this article, we'll explore how you can use PHPUnit and PEST to write tests for your Laravel app.

PHPUnit: The Foundation of Testing in Laravel

PHPUnit is the de facto standard for unit testing PHP applications. Laravel is built with testing in mind, and it includes out-of-the-box support for PHPUnit with a phpunit.xml configuration file and some base test classes.

Writing Your First PHPUnit Test

When you install Laravel, it comes with an example test file. To write a new test, you can create a file within the tests/Feature or tests/Unit directory, depending on the type of test you're writing. Here's an example of a feature test that checks if the blog posts index page is accessible:

<?php

namespace Tests\Feature;

use Illuminate\Foundation\Testing\RefreshDatabase;
use Tests\TestCase;

class PostTest extends TestCase
{
    use RefreshDatabase;

    /** @test */
    public function posts_index_page_can_be_accessed()
    {
        $response = $this->get('/posts');

        $response->assertStatus(200);
    }
}

Or we can simplify it with this:

<?php

namespace Tests\Feature;

use Illuminate\Foundation\Testing\RefreshDatabase;
use Tests\TestCase;

class PostTest extends TestCase
{
    use RefreshDatabase;

    /** @test */
    public function posts_index_page_can_be_accessed()
    {
		$this->get('/posts')->assertStatus(200);
    }
}

You can run your tests using the following command:

php artisan test

Testing Database Interactions

Laravel provides traits like RefreshDatabase that you can use within your tests to reset your database after each test. This ensures that your tests are independent and won't affect each other.

Here's a test that ensures a new post can be created:

<?php

namespace Tests\Feature;

use Illuminate\Foundation\Testing\RefreshDatabase;
use Tests\TestCase;

/** @test */
public function a_new_post_can_be_created()
{
	$user = User::factory()->create(); // First we create a user

    $postData = [
        'title' => 'A New Post',
        'content' => 'Content of the new post',
    ];

	// This stimulates authentication when creating the new post
	$this->actingAs($user)
		 ->post('/posts', $postData);

    $this->assertDatabaseHas('posts', [
		 'user_id' => $user->id,
		 'title' => 'A New Post',
		 'content' => 'Content of the new post',
	]);
}

PEST: A Fresh Approach to Testing in Laravel

PEST is a testing framework that works on top of PHPUnit, offering a different approach with a focus on simplicity and a clean syntax. PEST allows you to write tests more expressively and elegantly.

Getting Started with PEST

To start using PEST in your Laravel blog, you'll need to create the test with the --pest flag:

php artisan make:test UserTest --pest

Writing Your First PEST Test

After installing PEST, you can write more concise tests. Here's the same test we wrote with PHPUnit, now with PEST:

<?php

namespace Tests\Feature;

use Tests\TestCase;

class PostTest extends TestCase
{
	it('can access posts index page', function () {
		$response = get('/posts');

		$response->assertStatus(200);
	});
}

To run your PEST tests, you can use the same commands as for PHPUnit tests:

php artisan test

Leveraging PEST Features

PEST supports all PHPUnit assertions, but it also provides additional features and helpers that make testing more enjoyable. For example, you can use higher-order tests to reduce boilerplate:

<?php

namespace Tests\Feature;

use Tests\TestCase;

class PostTest extends TestCase
{
	it('can access posts index page')->get('/posts')->assertStatus(200);
}

Conclusion

Testing is an integral part of the development cycle, and Laravel provides robust tools to help you ensure that your blog functions correctly. Whether you prefer the familiarity of PHPUnit or the expressive syntax of PEST, Laravel has got you covered.

With PHPUnit, you have a powerful and well-established testing framework at your disposal. If you're looking for a more modern and elegant approach, PEST is an excellent choice that can make your tests even more readable and enjoyable to write.

Upcoming Articles in the Series

  1. Laravel for Beginners: Blade and Breeze

  2. Laravel for Beginners: Service Providers

  3. Laravel for Beginners: Form Requests and Validation Rules


Bring Your Ideas to Life 🚀

If you need help with a Laravel project let's get in touch.

Lucky Media is proud to be recognized as a Top Laravel Development Agency

Arlind Musliu Portrait
Arlind Musliu

Cofounder and CFO of Lucky Media

Technologies:

Laravel
Heading Pattern

Related Posts

Stay up to date

Be updated with all news, products and tips we share!