How to execute specific test class using PHPUnit in Laravel

Stefan Izdrail

Founder & Senior Architect · 2026-06-29

Laravel Company
Title: Executing Specific Test Classes Using PHPUnit in Laravel Projects In this blog post, we will explore how to execute a particular test class using PHPUnit in your Laravel project. As you might already know, PHPUnit is an essential tool for testing and ensuring that code quality and functionality are at their best. With multiple testing classes within a Laravel project, it's crucial to be able to run only specific tests when needed. Before diving into the steps, let us first take a look at our test class structure:
<?php
use App\Repositories\ApplicationVersionFormat;

class ApplicationVersionFormatTest extends \TestCase
{
  public function testValidFormat()
  {
    $version = '1.2.3';
    $appVersion = new ApplicationVersionFormat();
    $this->assertEquals(true,$appVersion->isValidVersion($version));
  }

   ...
}
Now let's proceed with executing a specific test class. Remember that PHPUnit uses the file path to look for the test class. In our case, the folder and file structure is as follows:
<?php
use App\Repositories\ApplicationVersionFormat;

class ApplicationVersionFormatTest extends \TestCase
{
  ...
}
The test class resides within the `test/repositories` folder, with its name as `ApplicationVersionFormat.php`. Based on this information, we can run the following PHPUnit command:
"test\repositories\ApplicationVersionFormatTest"
However, you may face issues if your application uses namespaces or is in a more complex structure. In that case, you'll need to use an absolute path for the test class location. Here's how it would look like:
"absolute/path/to/test/repositories/ApplicationVersionFormatTest"
Remember that PHPUnit can also run a specific functionality inside your test classes using filters and test suites. In our example, assume you want to execute only the `testValidFormat()` method within the `ApplicationVersionFormatTest` class:
"test\repositories\ApplicationVersionFormatTest::testValidFormat"
However, this command might not be effective due to the possibility of the test case having multiple methods with a similar name. To avoid confusion and ensure accurate execution, it's recommended to use a specific method name directly in the class name. In our case:
"test\repositories\ApplicationVersionFormatTest-ValidFormat"
Now let us assume we have another test class within our project, for example:
<?php
use App\Repositories\TransactionRepository;

class TransactionRepositoryTest extends \TestCase
{
  public function testCreateTransaction()
  {
    $transaction = new Transaction();
    $transaction->create();
    $this->assertEquals(true, $transaction->isSaved());
  }
}
In this case, the folder and file structure would be:
<?php
use App\Repositories\ApplicationVersionFormat;

class ApplicationVersionFormatTest extends \TestCase
{
  ...
}
<?php
use App\Repositories\TransactionRepository;

class TransactionRepositoryTest extends \TestCase
{
  public function testCreateTransaction()
  {
    $transaction = new Transaction();
    $transaction->create();
    $this->assertEquals(true, $transaction->isSaved());
  }
}
The file hierarchy would be:
"test/repositories\ApplicationVersionFormat.php" and "test/repositories\TransactionRepository.php"
To execute the `TransactionRepositoryTest` class, the PHPUnit command would be:
"test\repositories\TransactionRepositoryTest"
Within a Laravel project, test classes are organized within their respective folders. When executing a test, it's essential to use the correct folder name and file path to ensure that PHPUnit locates your desired class. To get the best results, create new test classes with clear names and follow the standard naming conventions for better organization and maintenance of your Laravel project. Remember that using filters or test suites can help you run a specific function within your test class. However, ensuring accurate execution by specifying a particular method name in the class name is more reliable. Always maintain structured and well-documented test classes to facilitate executing only specific tests when necessary.