You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

寻求Laravel Dusk测试结果与TestRail集成的实现方案

Laravel Dusk 与 TestRail 集成实现方案

目前没有官方或成熟的第三方包直接实现两者集成,但可以通过TestRail的API手动同步Dusk测试结果,以下是分步实现方案:

1. 获取TestRail API凭证

  • 登录TestRail,进入个人设置页面生成并复制API密钥
  • 记录TestRail实例的基础URL(如https://your-testrail-instance.testrail.io/)、你的登录邮箱(API用户名)、目标项目ID和测试套件ID

2. 配置Laravel环境变量

在.env文件中添加以下配置项:

TESTRAIL_URL=https://your-testrail-instance.testrail.io/
TESTRAIL_USER=your-email@example.com
TESTRAIL_API_KEY=your-api-key
TESTRAIL_PROJECT_ID=1
TESTRAIL_SUITE_ID=2
TESTRAIL_ENABLED=true # 本地开发可设为false关闭上报

同时在config/app.php中添加对应配置映射:

'testrail_url' => env('TESTRAIL_URL'),
'testrail_user' => env('TESTRAIL_USER'),
'testrail_api_key' => env('TESTRAIL_API_KEY'),
'testrail_project_id' => env('TESTRAIL_PROJECT_ID'),
'testrail_suite_id' => env('TESTRAIL_SUITE_ID'),
'testrail_enabled' => env('TESTRAIL_ENABLED', false),

3. 封装TestRail API服务类

创建app/Services/TestRailService.php,封装结果上报的核心逻辑:

<?php

namespace App\Services;

use Illuminate\Support\Facades\Http;

class TestRailService
{
    protected $baseUrl;
    protected $auth;
    protected $runId;

    public function __construct()
    {
        if (!config('app.testrail_enabled')) {
            return;
        }

        $this->baseUrl = rtrim(config('app.testrail_url'), '/') . '/index.php?/api/v2/';
        $credentials = base64_encode(config('app.testrail_user') . ':' . config('app.testrail_api_key'));
        $this->auth = "Basic {$credentials}";
        $this->runId = $this->getOrCreateTestRun();
    }

    // 上报单个测试用例结果
    public function submitTestResult(int $caseId, int $statusId, string $comment = '')
    {
        if (!config('app.testrail_enabled')) {
            return;
        }

        $endpoint = "add_result_for_case/{$this->runId}/{$caseId}";

        Http::withHeaders([
            'Authorization' => $this->auth,
            'Content-Type' => 'application/json'
        ])->post($this->baseUrl . $endpoint, [
            'status_id' => $statusId,
            'comment' => $comment
        ]);
    }

    // 获取现有测试运行或创建新运行
    protected function getOrCreateTestRun(): int
    {
        // 可根据需求调整:比如查找当天的运行,不存在则创建
        $response = Http::withHeaders([
            'Authorization' => $this->auth,
            'Content-Type' => 'application/json'
        ])->post($this->baseUrl . 'add_run/' . config('app.testrail_project_id'), [
            'suite_id' => config('app.testrail_suite_id'),
            'name' => 'Laravel Dusk 自动测试运行 - ' . now()->format('Y-m-d H:i:s')
        ]);

        return $response->json()['id'];
    }
}

4. 在Dusk测试中集成结果上报

修改你的Dusk测试类,利用PHPUnit的生命周期钩子捕获测试结果并上报:

<?php

namespace Tests\Browser;

use App\Services\TestRailService;
use Laravel\Dusk\Browser;
use Tests\DuskTestCase;

class ExampleTest extends DuskTestCase
{
    // 对应TestRail中的测试用例ID
    protected $testRailCaseId = 123;

    protected function tearDown(): void
    {
        parent::tearDown();

        $testRail = new TestRailService();
        $statusId = $this->getStatus() === \PHPUnit\Framework\TestStatus::STATUS_PASSED ? 1 : 5;
        $comment = $this->getFailureComment();

        $testRail->submitTestResult($this->testRailCaseId, $statusId, $comment);
    }

    // 获取测试失败时的错误信息
    protected function getFailureComment(): string
    {
        if ($this->getStatus() !== \PHPUnit\Framework\TestStatus::STATUS_PASSED) {
            $failures = $this->getTestResultObject()->failures();
            return !empty($failures) ? $failures[0]->getException()->getMessage() : '测试执行失败';
        }

        return '测试通过';
    }

    public function testBasicExample()
    {
        $this->browse(function (Browser $browser) {
            $browser->visit('/')
                    ->assertSee('Laravel');
        });
    }
}

注意事项

  • 确保TestRail中的测试用例ID与代码中$testRailCaseId一一对应,避免结果上报错误
  • 本地开发时可将TESTRAIL_ENABLED设为false,避免不必要的API请求
  • 建议添加日志记录,捕获API请求失败的情况,避免影响测试流程
  • 若需批量上报结果,可扩展TestRailService实现add_results接口的调用逻辑

内容的提问来源于stack exchange,提问作者Donna Esperas

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.29 11:22:29