Skip to content

Run the tests of a module

At the end, php artisan test runs tests across all modules, and you can run tests for a single module on demand. Prerequisite: a Blog module (see Create a module).

Create a feature test in the Blog module:

Terminal window
php artisan module:make-test PostIndexTest Blog --feature

This writes Modules/Blog/tests/Feature/PostIndexTest.php. Omit --feature to create a unit test in Modules/Blog/tests/Unit/.

Always end the class name with Test. The generator does not append it, and PHPUnit only discovers files ending in Test.php.

Generated tests extend your application’s Tests\TestCase, so they boot the full application. Add RefreshDatabase to migrate both application and module tables during tests:

Modules/Blog/tests/Feature/PostIndexTest.php
<?php
namespace Modules\Blog\Tests\Feature;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Tests\TestCase;
class PostIndexTest extends TestCase
{
use RefreshDatabase;
public function test_the_application_returns_a_successful_response(): void
{
$response = $this->get('/');
$response->assertStatus(200);
}
}

Pass a file path to run one test, or the module’s tests folder to run all of its tests:

Terminal window
php artisan test Modules/Blog/tests/Feature/PostIndexTest.php
php artisan test Modules/Blog/tests

By default, phpunit.xml includes only tests/Unit and tests/Feature, so php artisan test skips Modules/.

  1. Add the module test globs to phpunit.xml.

    phpunit.xml
    <testsuites>
    <testsuite name="Unit">
    <directory>tests/Unit</directory>
    <directory>Modules/*/tests/Unit</directory>
    </testsuite>
    <testsuite name="Feature">
    <directory>tests/Feature</directory>
    <directory>Modules/*/tests/Feature</directory>
    </testsuite>
    </testsuites>
  2. Run the test suite.

    Terminal window
    php artisan test

Add --testsuite=Feature to run a single suite.

The output includes Modules\Blog\Tests\Feature\PostIndexTest and reports a passing run.