单元测试 #
一、测试概述 #
1.1 测试类型 #
text
Laravel测试类型
├── 单元测试
│ └── 测试单个类或方法
├── 功能测试
│ └── 测试应用功能
├── HTTP测试
│ └── 测试HTTP请求响应
└── 浏览器测试
└── 使用Dusk进行浏览器测试
1.2 测试目录 #
text
tests/
├── Feature/ # 功能测试
│ └── ExampleTest.php
├── Unit/ # 单元测试
│ └── ExampleTest.php
├── CreatesApplication.php
└── TestCase.php # 基础测试类
二、创建测试 #
2.1 创建测试类 #
bash
# 创建单元测试
php artisan make:test UserTest --unit
# 创建功能测试
php artisan make:test UserTest
2.2 测试类结构 #
php
namespace Tests\Unit;
use PHPUnit\Framework\TestCase;
class UserTest extends TestCase
{
public function test_example()
{
$this->assertTrue(true);
}
}
三、运行测试 #
3.1 运行所有测试 #
bash
php artisan test
# 或使用PHPUnit
./vendor/bin/phpunit
3.2 运行指定测试 #
bash
# 运行单个文件
php artisan test tests/Unit/UserTest.php
# 运行指定方法
php artisan test --filter test_example
四、HTTP测试 #
4.1 基本请求 #
php
namespace Tests\Feature;
use Tests\TestCase;
class UserTest extends TestCase
{
public function test_index_returns_success()
{
$response = $this->get('/users');
$response->assertStatus(200);
}
public function test_store_creates_user()
{
$response = $this->post('/users', [
'name' => 'John',
'email' => 'john@example.com',
]);
$response->assertStatus(201);
}
}
4.2 断言方法 #
php
// 状态码断言
$response->assertStatus(200);
$response->assertOk();
$response->assertCreated();
$response->assertNotFound();
// JSON断言
$response->assertJson(['name' => 'John']);
$response->assertJsonPath('data.name', 'John');
$response->assertJsonStructure(['data' => ['id', 'name']]);
// 重定向断言
$response->assertRedirect('/users');
// 视图断言
$response->assertViewIs('users.index');
$response->assertViewHas('users');
五、认证测试 #
5.1 模拟认证用户 #
php
use App\Models\User;
public function test_authenticated_user_can_access_dashboard()
{
$user = User::factory()->create();
$response = $this->actingAs($user)
->get('/dashboard');
$response->assertStatus(200);
}
六、模型工厂 #
6.1 创建工厂 #
bash
php artisan make:factory UserFactory
6.2 使用工厂 #
php
// 创建单个用户
$user = User::factory()->create();
// 创建多个用户
$users = User::factory()->count(3)->create();
// 覆盖属性
$user = User::factory()->create([
'email' => 'test@example.com',
]);
七、数据库测试 #
7.1 使用RefreshDatabase #
php
use Illuminate\Foundation\Testing\RefreshDatabase;
class UserTest extends TestCase
{
use RefreshDatabase;
public function test_create_user()
{
$user = User::factory()->create();
$this->assertDatabaseHas('users', [
'email' => $user->email,
]);
}
}
7.2 数据库断言 #
php
// 检查数据存在
$this->assertDatabaseHas('users', ['email' => 'john@example.com']);
// 检查数据不存在
$this->assertDatabaseMissing('users', ['email' => 'john@example.com']);
// 检查数量
$this->assertDatabaseCount('users', 10);
八、模拟 #
8.1 模拟Facade #
php
use Illuminate\Support\Facades\Mail;
public function test_mail_sent()
{
Mail::fake();
// 执行发送邮件代码
Mail::assertSent(WelcomeEmail::class);
}
8.2 模拟事件 #
php
use Illuminate\Support\Facades\Event;
public function test_event_dispatched()
{
Event::fake();
// 执行代码
Event::assertDispatched(UserRegistered::class);
}
8.3 模拟队列 #
php
use Illuminate\Support\Facades\Queue;
public function test_job_dispatched()
{
Queue::fake();
// 执行代码
Queue::assertPushed(SendEmail::class);
}
九、总结 #
9.1 核心要点 #
| 要点 | 说明 |
|---|---|
| RefreshDatabase | 重置数据库 |
| actingAs | 模拟认证用户 |
| Factory | 模型工厂 |
| assertStatus | 状态码断言 |
| assertJson | JSON断言 |
| fake() | 模拟Facade |
9.2 下一步 #
掌握了单元测试后,让我们继续学习 部署上线,了解Laravel应用部署!
最后更新:2026-03-28