Hello! Welcome to the final lesson in our module on Production-Ready Testing.
Throughout this module, you've learned to write unit tests, feature tests, and database tests. In our last lesson, you mastered the use of mocks and fakes to isolate your application from external services, ensuring your tests are fast and reliable. Now, it's time to zoom out from individual test files and look at the big picture.
Today, we'll address the learning outcome: Configure and run a full test suite using PHPUnit or Pest. We'll cover how to set up a dedicated testing environment, how to execute your tests using various commands, and how to leverage Pest for a more expressive testing syntax. We'll also touch on advanced features like parallel testing to make your entire suite faster and more efficient.
By the end of this lesson, you'll be able to manage and execute the entire collection of tests you've learned to write.
1. Configuring Your Testing Environment
A crucial principle of testing is to never run your tests against your live development database. Doing so can lead to data corruption and unpredictable test results. Laravel provides a clean, isolated environment for testing, which you configure primarily through two files: phpunit.xml and .env.testing.
The phpunit.xml File
This file is the main configuration entry point for PHPUnit, the testing framework that powers both standard PHPUnit tests and Pest. It defines your test suites and allows you to set server-wide environment variables for testing.

One of its most important jobs is to set the APP_ENV variable to testing. Laravel detects this and automatically adjusts other services, like switching the cache and session drivers to array to prevent data persistence between tests.
Setting Up a Test Database
You have two primary ways to tell Laravel which database to use for testing.
- Directly in
phpunit.xml: You can uncomment and set theDB_CONNECTIONandDB_DATABASEvariables within the<php>section of yourphpunit.xmlfile. - Using
.env.testing: You can create a.env.testingfile in your project's root. When you run your tests, Laravel will automatically use this file instead of your regular.envfile. This is often the preferred method as it keeps all environment-specific credentials out of version control.
The following video provides an excellent walkthrough of both methods.
Laravel Testing 04/24: Database Configuration: RefreshDatabase, Phpunit.xml and .env.testing
The Laravel Daily channel demonstrates how to configure a separate database for your tests, a critical step for isolating your test runs.
Watch the video from 00:11 to 05:05. Pay close attention to: How phpunit.xml is used to set the database connection (e.g., to an in-memory SQLite database). The purpose of the RefreshDatabase trait (which should be a familiar concept from our database testing lesson). How to use the .env.testing file as a more flexible alternative.
A Note on Your Goal: The video primarily uses SQLite for testing because it's fast and simple. However, given your goal to master MSSQL, the best practice for you is to create a separate, empty database on your MSSQL server (e.g., my_app_test). Then, in your .env.testing file, you would configure your application to use it:
DB_CONNECTION=sqlsrv
DB_HOST=127.0.0.1
DB_PORT=1433
DB_DATABASE=my_app_test
DB_USERNAME=sa
DB_PASSWORD=your_password
This ensures your tests run against the exact same database engine you use in production, which can prevent subtle bugs caused by differences in SQL dialects between database systems.
2. Running Your Test Suite
Once your environment is configured, you can run your tests. Laravel offers a few ways to do this.
The official Laravel documentation outlines the different commands available for executing your tests.
Read the section titled 'Running Tests'. It's short and to the point, covering the main commands.
To summarize the key commands:
./vendor/bin/phpunit: The direct command to run the PHPUnit test runner../vendor/bin/pest: The command to run the Pest test runner (more on this next).php artisan test: Laravel's own test runner. This is often the most convenient command, as it provides more verbose output and a cleaner interface.
Any arguments you would pass to phpunit or pest can also be passed to php artisan test. For example, to run only your Feature tests:
php artisan test --testsuite=Feature
To run a single specific test file:
php artisan test tests/Feature/MySpecificTest.php
To run a single test method within a file, you can use the --filter option:
php artisan test --filter="it authenticates a user"

3. An Introduction to Pest
While all tests in Laravel rely on PHPUnit under the hood, you are not limited to writing tests in PHPUnit's class-based style. Pest is a popular testing framework built on top of PHPUnit that focuses on simplicity and readability, using a more functional, closure-based syntax.
Let's watch a video that introduces Pest and shows how to convert a standard PHPUnit test.
Pest - An Elegant PHP Testing Framework
This video by Andre Madarang provides a fantastic introduction to Pest, from installation to converting your first test.
Watch from 00:03 to 06:29. Focus on: The core idea behind Pest: a simpler, more expressive syntax. The installation process and running tests with the pest command. How a PHPUnit test class is converted to a Pest file using the it() function and closures. The concept of the tests/Pest.php file for applying global configurations, like the RefreshDatabase trait.
As the video demonstrates, Pest can make your tests feel more descriptive. Compare the syntax:
PHPUnit:
use Tests\TestCase;
use Illuminate\Foundation\Testing\RefreshDatabase;
class ExampleTest extends TestCase
{
use RefreshDatabase;
public function test_the_application_returns_a_successful_response(): void
{
$response = $this->get('/');
$response->assertStatus(200);
}
}
Pest:
// No class needed
it('returns a successful response for the home page', function () {
$this->get('/')->assertStatus(200);
});
Centralizing Configuration with Pest.php
One of Pest's most powerful features is the tests/Pest.php file. Instead of adding use RefreshDatabase; to every test file that needs it, you can apply it globally to an entire directory.
The official Pest documentation explains how to use the Pest.php file to configure your test suite.
Read the introduction to understand the purpose of Pest.php. Then, skip down and read the section that starts with 'A trait can be linked to a test or folder...'. This shows the exact syntax for globally applying traits.
For instance, to apply the RefreshDatabase trait to all tests inside your tests/Feature directory, you would add this to tests/Pest.php:
// tests/Pest.php
use Illuminate\Foundation\Testing\RefreshDatabase;
use Tests\TestCase;
// Uses the given test case class as a base for all Feature tests
pest()->extend(TestCase::class)->in('Feature');
// Uses the RefreshDatabase trait in all tests in the 'Feature' directory
pest()->use(RefreshDatabase::class)->in('Feature');
This keeps your individual test files leaner and focused only on the test logic itself.
4. Advanced Test Runner Features
The php artisan test command comes with some powerful flags that help you manage and optimize a large test suite.
Let's revisit the Laravel documentation to explore some of these advanced features.
Quickly scan the sections 'Running Tests in Parallel', 'Reporting Test Coverage', and 'Profiling Tests'. You don't need to memorize them, but understand what each feature does.
Here's a quick summary of the most useful options:
-
Parallel Testing (
--parallel): This is a game-changer for large test suites. It runs your tests across multiple CPU cores simultaneously, which can dramatically reduce the total runtime. Laravel automatically handles creating a separate test database for each parallel process.php artisan test --parallel -
Code Coverage (
--coverage): This option generates a report showing what percentage of your application code is executed by your tests. It helps you identify parts of your codebase that are not well-tested. This requires Xdebug or PCOV to be installed.php artisan test --coverage -
Profiling (
--profile): This lists the 10 slowest tests in your suite. It's incredibly useful for identifying bottlenecks and optimizing test performance.php artisan test --profile
Conclusion
Congratulations, you've reached the end of the module on Production-Ready Testing! You now possess a comprehensive set of skills to ensure your Laravel applications are robust, reliable, and maintainable.
Key Takeaways:
- Configuration is Key: Always use a dedicated testing environment, configured via
phpunit.xmland.env.testing, to protect your development data. - Run with Artisan: The
php artisan testcommand is your primary tool for running tests, offering clear output and powerful options. - Pest Offers Elegance: Pest provides a clean, expressive syntax for writing tests and simplifies configuration with the
tests/Pest.phpfile. - Optimize Your Suite: For large projects, use features like parallel testing (
--parallel) and profiling (--profile) to keep your test suite fast and efficient.
Up Next:
With a strong foundation in automated testing, you're now equipped to build high-quality applications. We will now shift gears to focus on a critical aspect of modern web development: APIs. In our next module, API Development: Resources & Versioning, we will begin by learning how to build API Resource classes to control and standardize your JSON responses. This is the first step toward creating professional, clean, and easy-to-consume APIs.