Courses

Testing in Laravel 11: Advanced Level

Customize Default Test Stubs

Summary of this lesson:
- Publish and modify test stub templates
- Customize default test generation
- Change default test content
- Use artisan stub:publish command
- Modify Pest and PHPUnit test templates

Generally, when you run php artisan make:test, it adds an example test, which is pretty meaningless. So, let's talk about generating a new test class with our custom code.


For example, let's generate a new test.

php artisan make:test SomeTest

When you open that test file, you see an example test that has been added.

tests/Feature/SomeTest.php:

<?php
 
test('example', function () {
$response = $this->get('/');
 
$response->assertStatus(200);
});

When using PHPUnit, the generated test looks like the one below. Comments were also added for the test.

tests/Feature/SomeTest.php:

<?php
 
namespace Tests\Feature;
 
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Foundation\Testing\WithFaker;
use Tests\TestCase;
 
class SomeTest extends TestCase
{
/**
* A basic feature test example.
*/
public function test_example(): void
{
$response = $this->get('/');
 
$response->assertStatus(200);
}
}

What if you want to avoid making...

The full lesson is only for Premium Members.
Want to access all 31 lessons of this course? (74 min read)

You also get:

  • 69 courses (majority in latest Laravel 11)
  • Premium tutorials
  • Access to repositories
  • Private Discord