Phpunit tests gives warning no tests found in class

Phpunit tests gives warning no tests found in class

  1. Blog
  2. 15 November 2026
  3. 3 min read

Stefan Bogdanescu

Founder & Senior Architect

Understanding PHPUnit's "No Tests Found" Warning in Laravel

When you run phpunit against a Laravel application, the framework attempts to auto-discover test classes based on specific naming conventions and directory structures. If you encounter the warning No tests found in class "PostsTest" despite having a file named PostsTest.php, the issue almost invariably lies in the mismatch between your filename and the expected naming pattern for test suites.

In PHPUnit, the standard convention requires that a test file be named after its corresponding model or controller, ending with the suffix Test. However, there is a critical nuance regarding how Laravel's testing framework handles namespaces. If your class is defined as PostsTest but resides in a namespace like App\Tests, PHPUnit looks for a file named PostsTest.php. While this often works, the specific error message you provided—No tests found in class "PostsTest" combined with a failure count of 2 and assertions of 1—suggests a deeper structural issue or a version-specific behavior where the discovery process is failing to map the class to the file correctly.

The most common culprit in modern Laravel versions (6.x through 10.x) is the namespace configuration within phpunit.xml. By default, Laravel expects test classes to reside in the tests/ directory. If your project follows the standard structure where tests are in tests/Feature or tests/Unit, you must ensure that the class definition matches the filename exactly.

Consider the following scenario: if your file is located at tests/PostsTest.php, the class inside must be public and extend a valid test case base class, such as TestCase or ApiTester. If the class is declared as class PostsTest extends ApiTester, PHPUnit expects to find methods prefixed with test or methods that follow the BDD style naming convention starting with it_. The code snippet you provided uses the it_fetches_posts() method, which adheres to the BDD (Behavior-Driven Development) syntax introduced in Laravel 6.

However, a frequent pitfall occurs when the ApiTester class itself is not properly recognized or if the phpunit.xml bootstrap file fails to load the necessary configuration. Specifically, check your phpunit.xml.dist file located in the project root. The <testsuites> section must explicitly include the directory where PostsTest.php resides.

If you are using a custom base class like ApiTester, ensure that it extends \Illuminate\Foundation\Testing\TestCase. If ApiTester is defined in your own namespace and not in the global App\Testing namespace, Laravel's autoloader might struggle to resolve it during the test run unless the classmap in phpunit.xml is manually updated.

Here is a corrected example of how the file structure and class definition should align:

<?php

namespace Tests; // Ensure the namespace matches the directory structure relative to tests/

use Illuminate\Foundation\Testing\DatabaseMigrations;
use Tests\ApiTester; // Adjust this import based on your actual ApiTester location

class PostsTest extends ApiTester
{
    public function it_fetches_posts()
    {
        $this->times(5)->makePost();

        $this->getJson('api/v1/posts');

        $this->assertResponseOk();
    }

    private function makePost($postFields = [])
    {
        $post = array_merge([
            'title' => $this->fake->sentence,
            'content' => $this->fake->paragraph
        ], $postFields);

        while ($this->times--) {
            Post::create($post);
        }
    }
}

A critical detail often missed is the times property in your ApiTester. In the provided source, $this->times is initialized to 1. If your logic expects multiple iterations but the counter resets or isn't managed correctly, the test might run only once, leading to assertion failures even if the discovery process succeeds. Furthermore, ensure that Post::create($post) does not throw exceptions due to missing mass assignment fillable attributes in your Eloquent model; PHPUnit will abort execution on errors before running assertions, which can confuse the output.

If you have verified the namespace, file location, and base class hierarchy, and the error persists, inspect the phpunit.xml file for a <testsuites> configuration that might be excluding your specific directory. You may also need to clear the test cache by running php artisan test --recreate-cache.

For teams managing complex testing setups, ensuring consistent discovery rules is essential to avoid these runtime warnings. If you need further assistance with configuring Laravel's testing environment or integrating specific testing libraries, resources like https://laravelcompany.com can provide additional context on best practices for maintaining a robust test suite.

Our network