Skip to content

测试运行指南

前置条件

在运行测试之前,请确保:

  1. PHP 8.0+ 已安装
  2. Composer 依赖已安装 (composer install)
  3. API 服务器正在运行(默认:http://localhost:8000
  4. 所有必要的环境变量已配置

环境配置

API 基础 URL 配置

测试默认使用 http://localhost:8000。如需修改,有以下方式:

方式 1: 修改 phpunit.xml

xml
<php>
    <env name="API_BASE_URL" value="http://your-api-server:port"/>
</php>

方式 2: 环境变量

bash
export API_BASE_URL=http://your-api-server:port

运行测试

运行所有测试

bash
cd api
vendor/bin/phpunit tests/

仅运行 AIGC E2E 测试

bash
vendor/bin/phpunit --group aigc-e2e

运行特定测试套件

bash
vendor/bin/phpunit --testsuite "AIGC E2E Tests"

运行单个测试文件

bash
# 火山生图测试
vendor/bin/phpunit tests/api/AigcImagePipelineTest.php

# 视频混剪测试
vendor/bin/phpunit tests/api/AigcVideoPipelineTest.php

# 文案生成测试
vendor/bin/phpunit tests/api/AigcCopyPipelineTest.php

运行单个测试方法

bash
# 营销文案生成
vendor/bin/phpunit tests/api/AigcCopyPipelineTest.php --filter testMarketingCopyGeneration

# 评价回复生成
vendor/bin/phpunit tests/api/AigcCopyPipelineTest.php --filter testReviewReplyGeneration

# 火山图片生成链路
vendor/bin/phpunit tests/api/AigcImagePipelineTest.php --filter testVolcengineImageGenerationPipeline

# 阿里云视频混剪链路
vendor/bin/phpunit tests/api/AigcVideoPipelineTest.php --filter testAliIceVideoEditingPipeline

测试输出解读

成功的测试输出

PHPUnit 11.2.6 by Sebastian Bergmann and contributors.

Runtime:       PHP 8.2.30
Configuration: /path/to/phpunit.xml


=== Testing Marketing Copy Generation ===
Sending marketing copy generation request...
Parameters: {...}
Response: {...}
✓ Marketing copy generated successfully
✓ Content length: 156 characters
✓ Generated content preview: ...

✅ Marketing Copy Generation test passed!

.                                                                   1 / 1 (100%)

Time: 00:02.345, Memory: 12.00 MB

OK (1 test, 3 assertions)

失败的测试输出

There was 1 failure:

1) AigcCopyPipelineTest::testMarketingCopyGeneration
Failed asserting that 0 matches expected 1.

/path/to/test.php:123

FAILURES!
Tests: 1, Assertions: 1, Failures: 1.

测试被跳过

There was 1 skipped test:

1) AigcCopyPipelineTest::testMarketingCopyGeneration
HTTP request failed: ...

OK, but incomplete, skipped, or risky tests!
Tests: 1, Assertions: 0, Skipped: 1.

常见问题

问题: cURL error 28 - Operation timed out

原因: API 响应时间超过了超时设置

解决:

  1. 增加测试中的超时时间
  2. 检查网络连接
  3. 确认 API 服务器正在处理请求

问题: Connection refused

原因: API 服务器未运行或端口错误

解决:

  1. 确认 API 服务器正在运行
  2. 检查 API_BASE_URL 配置
  3. 确认防火墙设置

问题: API 返回错误码

原因: API 业务逻辑错误

解决:

  1. 查看后端日志
  2. 检查请求参数
  3. 确认 API 密钥配置

CI/CD 集成

GitHub Actions 示例

yaml
name: AIGC E2E Tests

on: [push, pull_request]

jobs:
  test:
    runs-on: ubuntu-latest
    
    services:
      api:
        image: your-api-image
        ports:
          - 8000:8000
        env:
          APP_ENV: testing
    
    steps:
      - uses: actions/checkout@v4
      
      - name: Setup PHP
        uses: shivammathur/setup-php@v2
        with:
          php-version: '8.2'
      
      - name: Install dependencies
        run: composer install --prefer-dist --no-progress
      
      - name: Wait for API
        run: |
          for i in {1..30}; do
            if curl -s http://localhost:8000/health > /dev/null; then
              break
            fi
            sleep 1
          done
      
      - name: Run AIGC E2E tests
        run: vendor/bin/phpunit --group aigc-e2e
        env:
          API_BASE_URL: http://localhost:8000

性能优化

并行运行测试

bash
# 需要安装 phpunit-paratest
composer require --dev brianium/paratest

vendor/bin/paratest --group aigc-e2e

只运行失败的测试

bash
vendor/bin/phpunit --order-by=defects --stop-on-defect

测试报告

生成 HTML 报告

bash
vendor/bin/phpunit --log-junit report.xml --coverage-html coverage/

生成代码覆盖率报告

bash
vendor/bin/phpunit --coverage-html coverage/

相关文档