SpeechRecognition/test/real/修复记录.md

4.1 KiB
Raw Blame History

端到端测试修复记录

2026-05-13 修复

问题 1视频大小限制测试失败

测试TestVideoFileValidation::test_video_file_size_within_limit

错误

AssertionError: 视频文件过大886.50MB
assert 929565696 <= 524288000

原因

  • 测试配置设置的限制为 500MB
  • 实际视频文件 VID_20251104_085655_024.AVI 大小为 886.50MB
  • 此限制是我添加的防御性测试,但项目实际没有文件大小限制

修复

# test_config.yaml
environment:
  max_file_size_mb: 1024  # 从 500MB 改为 1GB

说明

  • Flask 的 MAX_CONTENT_LENGTH (500MB) 用于 HTTP 上传限制
  • 本项目使用本地文件,不适用此限制
  • 测试限制仅用于验证,不应过于严格

问题 2文件不存在错误处理测试失败

测试TestErrorHandling::test_nonexistent_file_error

错误

assert response.status_code in [400, 404, 500]
assert 200 in [400, 404, 500]

原因

  • app/asr/core.py::main() 函数在文件不存在时返回错误字符串 "输入路径不存在"
  • app/asr/routes.py 没有检查返回值,直接返回 200 成功

代码问题

# routes.py (修复前)
try:
    from app.asr.core import main
    main(path)  # 返回值被忽略
finally:
    ...

return jsonify(make_response(...)), 200  # 总是返回 200

修复

# routes.py (修复后)
try:
    from app.asr.core import main
    result = main(path)
    
    # 检查返回值,如果是错误信息,返回 400
    if result and isinstance(result, str):
        task_running[task_id] = False
        return jsonify(make_response(status="error", message=result)), 400
        
finally:
    ...

影响

  • 现在文件不存在时正确返回 400 错误
  • API 错误处理更一致
  • 测试通过

问题 3测试超时导致进程终止

测试TestAPIResponseFormat::test_recognize_response_format

错误

The python test process was terminated before it could exit on its own

原因

  • 超时设置为 60 秒
  • 首次运行需要加载 ASR 模型(可能 30 秒 -2 分钟)
  • 9.16MB 音频文件推理也需要时间
  • 总时间超过 60 秒导致被强制终止

修复

# test_real_e2e.py
@pytest.mark.real
@pytest.mark.slow
@pytest.mark.timeout(300)  # 从 60 秒改为 300 秒5 分钟)
def test_recognize_response_format(self, e2e_app, test_audio_file):
    ...

说明

  • 首次运行:模型下载 + 加载 + 推理(可能需要 5-10 分钟)
  • 后续运行模型已缓存1-2 分钟)
  • 超时时间应该包含模型加载时间

修复总结

已修复的问题

问题 修复方式 文件
视频大小限制过严 500MB → 1GB test_config.yaml
文件不存在返回 200 检查 main() 返回值 app/asr/routes.py
测试超时被终止 60 秒 → 300 秒 test_real_e2e.py

修复后的预期行为

  1. 视频大小测试

    • 886MB 视频文件通过测试
    • 限制值 1GB 足够大
  2. 错误处理测试

    • 文件不存在返回 400
    • 错误信息正确传递
  3. 响应格式测试

    • 5 分钟超时足够完成首次推理
    • 进程不会被强制终止

运行建议

首次运行前预加载模型

# 避免测试时等待模型加载
python -c "from app.asr.asr_service import ASRService; ASRService()"

运行错误处理测试(快速验证)

pytest test/real/test_real_e2e.py::TestErrorHandling -v

运行完整测试(首次需要耐心)

pytest test/real/ -v -s

跳过慢速测试(只验证基本功能)

pytest test/real/ -v -m "not slow"

测试时间参考

测试类别 首次运行 后续运行
文件验证 <1 秒 <1 秒
错误处理 <1 秒 <1 秒
ASR 识别9MB 2-5 分钟 1-2 分钟
ASR 识别50MB 5-10 分钟 2-5 分钟
视频转码 1-3 分钟 1-3 分钟

注意首次运行包括模型下载10-30 分钟,取决于网络)