Back to Blog
Lesson 45 of the Laravel Fundamentals: From Zero to Your First App course
LaravelJune 25, 20263 min read

Introduction to Testing: Build Confident Laravel Apps

Stop manually refreshing your browser to verify features. Learn how to write your first Laravel feature test using PEST to automate assertions and save time.

LaravelPHPUnitPESTTestingTDDphpbackend

Previously in this course, we refactored our Task Manager: Refactoring for Clean Code to keep our controllers lean and maintainable. Now, we’ll move from manual browser testing to automated verification.

Testing is the difference between a project that works "on my machine" and one that works in production. By writing tests, you create a safety net that warns you immediately if a new feature breaks an existing one. In Laravel, we primarily use two tools for this: PHPUnit (the industry-standard engine) and PEST (a delightful, expressive testing framework built on top of PHPUnit).

Why Feature Testing?

In Laravel, we distinguish between Unit Tests (testing a single method in isolation) and Feature Tests (testing a full request-to-response cycle). For a beginner, Feature Tests are your best friend. They simulate a real user hitting your routes, filling out forms, and seeing data in the database.

If you’ve explored other ecosystems, you might recognize these concepts from Introduction to Testing: Quality Assurance in React with Jest, but here we apply them to server-side logic and database persistence.

Your First Feature Test

Laravel comes pre-installed with PEST. To create your first test, run this command in your terminal:

Bash
php artisan make:test TaskTest --pest

This generates a file in tests/Feature/TaskTest.php. Open it up, and you'll see a basic test structure. Let’s write a test to ensure our "Index" page loads correctly.

PHP
test('the tasks page returns a successful response', function () {
    $response = $this->get('/tasks');

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

Here, we are:

  1. Making a request: $this->get('/tasks') simulates a visitor hitting that URL.
  2. Asserting: assertStatus(200) checks that the server responded with an "OK" status.

To run this test, execute php artisan test in your terminal. You should see a green output confirming your test passed.

Testing Database State

Testing the response status is only the beginning. Often, you need to verify that your application is actually saving data to the database. We use the assertDatabaseHas assertion for this.

Let’s add a test to verify that we can create a task:

PHP
use App\Models\Task;

test('a user can create a task', function () {
    $this->post('/tasks', [
        'title' => 'Buy milk',
        'description' => 'Go to the store'
    ]);

    $this->assertDatabaseHas('tasks', [
        'title' => 'Buy milk',
    ]);
});

In this example, we send a POST request to our store route. Immediately after, we check the tasks table to ensure the record exists. Laravel automatically wraps these tests in a database transaction, meaning it rolls back the changes after the test finishes—your database stays clean!

Hands-on Exercise

  1. Open tests/Feature/TaskTest.php.
  2. Write a test that visits the /tasks route and asserts that the page contains the text "My Tasks" (you can use $response->assertSee('My Tasks')).
  3. Run php artisan test to confirm it passes.
  4. If it fails, check if your route or view actually contains that string.

Common Pitfalls

  • Forgetting the Database: If you try to test database interactions but your test isn't hitting the database, ensure your test class uses the Illuminate\Foundation\Testing\RefreshDatabase trait (though PEST usually handles this automatically).
  • The "Green Test" Trap: A test that always passes is useless. Always write a test that fails first, then write the code to make it pass. This is known as "Red-Green-Refactor."
  • Authentication: If your routes are protected by middleware (like we built in Task Manager: Securing the Application), your tests will fail with a 302 redirect. We will cover how to "login" as a user in our next lesson.

Recap

We've covered the basics of automated testing in Laravel. We used PEST to verify that our routes respond with a 200 status code and confirmed that our database operations successfully persist data. By mastering these basics, you're building a foundation for a robust, professional-grade application.

Up next: Testing Forms and Validation — where we'll simulate user input and ensure our validation rules actually catch bad data.

Similar Posts