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).
Generate a test
Section titled “Generate a test”Create a feature test in the Blog module:
php artisan module:make-test PostIndexTest Blog --featureThis 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:
<?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); }}Run the tests of one module
Section titled “Run the tests of one module”Pass a file path to run one test, or the module’s tests folder to run all of its tests:
php artisan test Modules/Blog/tests/Feature/PostIndexTest.phpphp artisan test Modules/Blog/testsRun module tests with application tests
Section titled “Run module tests with application tests”By default, phpunit.xml includes only tests/Unit and tests/Feature, so php artisan test skips Modules/.
-
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> -
Run the test suite.
Terminal window php artisan test
Add --testsuite=Feature to run a single suite.
Check the result
Section titled “Check the result”The output includes Modules\Blog\Tests\Feature\PostIndexTest and reports a passing run.