温馨提示×

Laravel项目Linux上如何测试

小樊
38
2025-11-16 17:58:16
栏目: 智能运维

Linux 上测试 Laravel 的完整流程

一 环境准备与初始化

  • 安装运行依赖:确保已安装与项目匹配的 PHP(≥7.3)Composer、数据库(如 MySQL/PostgreSQL)及 Nginx/Apache。在 Ubuntu/Debian 可用包管理器安装 PHP 扩展(如 php-fpm、php-mysql、php-mbstring、php-xml、php-zip 等)。
  • 安装依赖与生成密钥:在项目根目录执行
    • 安装:composer install(开发环境保留 dev 依赖;生产环境加 --no-dev 并做 optimize-autoloader
    • 密钥:php artisan key:generate
  • 配置环境:复制 .env.example.env,设置数据库连接与应用相关配置。
  • 测试专用配置:Laravel 会在运行测试时自动使用 testing 环境,并将 session/cache 驱动设为 array;如有自定义测试配置,确保 phpunit.xml 正确,并在需要时执行 php artisan config:clear 以避免缓存干扰。

二 运行测试与常用命令

  • 运行全部测试:在项目根目录执行
    • ./vendor/bin/phpunitphp artisan test(推荐,语义更清晰)
  • 运行指定用例或目录:
    • 按类:php artisan test --filter ExampleTest
    • 按目录:php artisan test tests/Featurephp artisan test tests/Unit
  • 并行与过滤:
    • 并行加速:php artisan test --parallel
    • 更多选项:php artisan test --help(可结合 --stop-on-failure--coverage 等)
  • 示例(Feature 测试):
    • 创建:php artisan make:test HomePageTest
    • 编写断言:
      <?php namespace Tests\Feature; use Tests\TestCase; class HomePageTest extends TestCase { public function test_homepage_status(): void { $response = $this->get('/'); $response->assertStatus(200); } } 
    • 运行:php artisan test --filter HomePageTest

三 测试类型与示例

  • 单元测试(tests/Unit):聚焦单一类/方法,不依赖 HTTP 或数据库。
    • 示例:
      <?php namespace Tests\Unit; use Tests\TestCase; use App\Models\Post; class PostTest extends TestCase { public function test_title_accessor(): void { $post = new Post(['title' => 'hello']); $this->assertEquals('Hello', $post->title); // 假设有访问器 ucfirst } } 
  • 功能测试(tests/Feature):通过 HTTP 请求测试路由、控制器、表单、JSON API 等。
    • 示例:
      <?php namespace Tests\Feature; use Tests\TestCase; class PostApiTest extends TestCase { public function test_can_create_post(): void { $payload = ['title' => 'Test', 'content' => 'Content']; $response = $this->postJson('/api/posts', $payload); $response->assertStatus(201)->assertJsonFragment($payload); } } 
  • 数据库相关测试:使用 DatabaseMigrationsDatabaseTransactions 管理测试数据。
    • 示例:
      <?php namespace Tests\Feature; use Illuminate\Foundation\Testing\DatabaseMigrations; use Tests\TestCase; class PostFeatureTest extends TestCase { use DatabaseMigrations; public function test_create_post_and_see_in_list(): void { $this->post('/posts', ['title' => 'A', 'content' => 'B']); $this->get('/posts')->assertSee('A'); } } 
  • 事件与监听器测试:在测试中触发事件并断言监听逻辑是否执行。
    • 示例:
      <?php namespace Tests\Feature; use App\Events\MyEvent; use Tests\TestCase; class EventTest extends TestCase { public function test_event_dispatched_and_listener_handled(): void { // 可结合 Mockery 断言监听被调用 event(new MyEvent('test')); // 断言数据库/缓存/日志等副作用 } } 
    上述示例展示了在 Linux 环境下使用 PHPUnitLaravel 测试工具进行单元、功能、数据库与事件的常见写法与运行方式。

四 数据库与存储测试要点

  • 使用内存 SQLite 提升速度(在 phpunit.xml<php> 段设置):
    <env name="DB_CONNECTION" value="sqlite"/> <env name="DB_DATABASE" value=":memory:"/> 
  • 使用事务回滚:在测试类上 use DatabaseTransactions; 可让每个测试在事务中运行,结束后自动回滚,避免污染数据。
  • 使用迁移:在测试类上 use DatabaseMigrations; 会在每个测试前执行迁移,适合需要真实表结构的场景。
  • 文件与存储:功能测试涉及上传时,可临时将 filesystems.disks.local.root 指向 storage/framework/testing,并在测试后清理。

五 性能与负载测试建议

  • 代码级基准:使用 Laravel Benchmark 测量关键代码段耗时。
    use Illuminate\Support\Facades\Benchmark; Benchmark::measure('Heavy job', function () { // 要测量的逻辑 }); 
  • 测试套件加速:使用 php artisan test --parallel 并行执行测试。
  • 外部负载工具:对接口进行压力与稳定性验证,可使用 Apache JMeterLocust 进行并发与场景压测。
  • 性能优化要点:在测试/预发环境验证 关闭 Debug、开启 OPcache、使用 路由/配置缓存、优化 数据库索引与查询、合理使用 缓存层

0