在 Laravel 中,测试是保证代码质量和稳定性的重要手段。Laravel 内置了强大的测试工具,支持单元测试、功能测试等多种类型的测试。下面将详细介绍如何在 Laravel 中进行测试,并给出示例代码。
确保你已经安装了 Laravel 项目。如果还没有安装,可以使用以下命令创建一个新的 Laravel 项目:
composer create-project --prefer-dist laravel/laravel laravel-testing-demo
cd laravel-testing-demo
Laravel 的测试文件默认存放在 tests
目录下,其中:
Feature
目录用于存放功能测试文件,功能测试通常用于测试应用的整个功能流程,比如测试路由、控制器等。Unit
目录用于存放单元测试文件,单元测试主要用于测试单个类或方法。可以使用以下命令运行所有测试:
php artisan test
单元测试主要用于测试单个类或方法。以下是一个简单的单元测试示例:
在 tests/Unit
目录下创建一个新的测试文件 ExampleUnitTest.php
:
<?php
namespace Tests\Unit;
use PHPUnit\Framework\TestCase;
class ExampleUnitTest extends TestCase
{
public function test_addition()
{
$result = 2 + 2;
$this->assertEquals(4, $result);
}
}
test_addition
是一个测试方法,PHPUnit 会自动识别以 test
开头的方法作为测试用例。$this->assertEquals(4, $result)
是一个断言,用于验证 $result
是否等于 4。php artisan test --testsuite=Unit
功能测试通常用于测试应用的整个功能流程,比如测试路由、控制器等。以下是一个简单的功能测试示例:
在 routes/web.php
中添加一个简单的路由:
use Illuminate\Support\Facades\Route;
Route::get('/hello', function () {
return 'Hello, World!';
});
在 tests/Feature
目录下创建一个新的测试文件 ExampleFeatureTest.php
:
<?php
namespace Tests\Feature;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Foundation\Testing\WithFaker;
use Tests\TestCase;
class ExampleFeatureTest extends TestCase
{
public function test_hello_route()
{
$response = $this->get('/hello');
$response->assertStatus(200);
$response->assertSee('Hello, World!');
}
}
$this->get('/hello')
发送一个 GET 请求到 /hello
路由,并返回响应对象。$response->assertStatus(200)
断言响应状态码是否为 200。$response->assertSee('Hello, World!')
断言响应内容是否包含 Hello, World!
。php artisan test --testsuite=Feature
在实际开发中,经常需要测试数据库操作。Laravel 提供了 RefreshDatabase
trait 来帮助我们在每次测试前重置数据库。
php artisan make:model Post -m
在生成的迁移文件中添加以下内容:
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class CreatePostsTable extends Migration
{
public function up()
{
Schema::create('posts', function (Blueprint $table) {
$table->id();
$table->string('title');
$table->text('content');
$table->timestamps();
});
}
public function down()
{
Schema::dropIfExists('posts');
}
}
运行迁移:
php artisan migrate
php artisan make:controller PostController
在 app/Http/Controllers/PostController.php
中添加以下内容:
<?php
namespace App\Http\Controllers;
use App\Models\Post;
use Illuminate\Http\Request;
class PostController extends Controller
{
public function store(Request $request)
{
$post = Post::create([
'title' => $request->title,
'content' => $request->content
]);
return response()->json($post, 201);
}
}
在 routes/api.php
中添加以下内容:
use App\Http\Controllers\PostController;
use Illuminate\Support\Facades\Route;
Route::post('/posts', [PostController::class, 'store']);
在 tests/Feature
目录下创建一个新的测试文件 PostControllerTest.php
:
<?php
namespace Tests\Feature;
use App\Models\Post;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Tests\TestCase;
class PostControllerTest extends TestCase
{
use RefreshDatabase;
public function test_create_post()
{
$data = [
'title' => 'Test Post',
'content' => 'This is a test post.'
];
$response = $this->postJson('/api/posts', $data);
$response->assertStatus(201);
$this->assertDatabaseHas('posts', $data);
}
}
use RefreshDatabase
用于在每次测试前重置数据库。$this->postJson('/api/posts', $data)
发送一个 POST 请求到 /api/posts
路由,并传递 $data
作为请求数据。$response->assertStatus(201)
断言响应状态码是否为 201。$this->assertDatabaseHas('posts', $data)
断言数据库中是否存在符合 $data
条件的记录。php artisan test --filter=PostControllerTest
通过以上步骤,你可以在 Laravel 中进行单元测试、功能测试和数据库操作测试。