测试基础设施(中文译文)
原始 DeepWiki 页面:https://deepwiki.com/open-webui/open-webui/17.2-testing-infrastructure
翻译时间:2026-06-09T16:12:04.777Z
翻译模型:deepseek-chat
原文字符数:10289
项目:Open WebUI (open-webui)
---
测试基础设施
相关源文件
以下文件被用作生成此 wiki 页面的上下文:
.github/workflows/build-release.yml.github/workflows/docker-build.yaml.github/workflows/format-backend.yaml.github/workflows/format-build-frontend.yaml.github/workflows/integration-test.disabled.github/workflows/lint-backend.disabled.github/workflows/lint-frontend.disabled.github/workflows/release-pypi.ymlbackend/open_webui/retrieval/web/firecrawl.pybackend/open_webui/retrieval/web/utils.pybackend/open_webui/storage/provider.pybackend/open_webui/test/apps/webui/storage/test_provider.pybackend/requirements-min.txtbackend/requirements.txtdocker-compose.playwright.yamlhatch_build.pypyproject.tomluv.lockvite.config.ts
本文档描述 Open WebUI 的测试基础设施,包括单元测试、集成测试、端到端测试以及测试框架。内容涵盖测试组织方式、执行策略以及整个代码库中前后端组件所使用的模拟方法。
---
测试框架概述
Open WebUI 采用多层测试策略。pytest 是 Python 后端代码的主要测试框架,而 Cypress 和 Vitest 用于测试 SvelteKit 前端。该基础设施支持多种数据库后端(SQLite、PostgreSQL、MariaDB)和云存储提供商(S3、GCS、Azure Blob Storage)。
测试组件栈
标题:测试基础设施架构
graph TB
subgraph "前端测试"
cypress["Cypress E2E"]
vitest["Vitest 单元测试"]
sonner["Svelte Sonner(Toast 验证)"]
end
subgraph "后端测试"
pytest["pytest"]
pytest_docker["pytest-docker"]
pytest_asyncio["pytest-asyncio"]
moto["moto[s3]"]
gcs_emulator["gcp-storage-emulator"]
end
subgraph "基础设施"
playwright["Playwright(网页加载)"]
docker_compose["Docker Compose 栈"]
ollama_test["Ollama(模拟/测试模型)"]
end
subgraph "测试目标"
chat_flow["Chat.svelte 流程"]
storage_providers["StorageProvider 类"]
migrations["Alembic/Peewee 迁移"]
api_endpoints["FastAPI 路由"]
end
cypress --> chat_flow
cypress --> api_endpoints
vitest --> chat_flow
pytest --> storage_providers
pytest --> api_endpoints
pytest_docker --> migrations
playwright --> chat_flow
ollama_test --> chat_flow
来源:
backend/requirements.txt:142-143(pytest 和 pytest-docker)pyproject.toml:218(pytest-asyncio)backend/requirements.txt:130(playwright).github/workflows/format-build-frontend.yaml:49-66.github/workflows/integration-test.disabled:14-61
---
测试依赖
后端核心测试包
Open WebUI 的后端测试基础设施定义在 backend/requirements.txt 和 pyproject.toml 中,并通过 GitHub Actions 进行验证。
| 包名 | 用途 |
|---|---|
pytest | 后端逻辑的主要测试框架 backend/requirements.txt:142。 |
pytest-asyncio | 为 FastAPI 和异步数据库驱动提供异步测试支持 pyproject.toml:218。 |
moto | 模拟 AWS 服务,特别是用于存储提供程序测试的 S3 backend/open_webui/test/apps/webui/storage/test_provider.py:6。 |
playwright | 浏览器自动化,用于网页内容加载和抓取验证 backend/requirements.txt:130。 |
ruff | Python 代码的 linting 和格式化强制检查 pyproject.toml:219。 |
前端测试工具
| 包名 | 用途 |
|---|---|
Cypress | 端到端测试和用户流程的浏览器自动化 .github/workflows/integration-test.disabled:53-61。 |
Vitest | Svelte 组件的前端单元测试 .github/workflows/format-build-frontend.yaml:65。 |
Prettier | Svelte 和 TypeScript 文件的代码格式化 .github/workflows/format-build-frontend.yaml:38。 |
来源:
backend/requirements.txt:140-143pyproject.toml:156-159.github/workflows/format-backend.yaml:41-46.github/workflows/format-build-frontend.yaml:38-66
---
存储提供程序测试
存储提供程序测试套件验证文件持久化的抽象层。测试位于 backend/open_webui/test/apps/webui/storage/test_provider.py,涵盖 LocalStorageProvider、S3StorageProvider、GCSStorageProvider 和 AzureStorageProvider。
测试实现细节
标题:存储提供程序测试逻辑
classDiagram
class TestLocalStorageProvider {
+test_upload_file()
+test_get_file()
+test_delete_file()
+test_delete_all_files()
}
class TestS3StorageProvider {
+s3_client: boto3.resource
+test_upload_file()
+test_delete_all_files()
}
class TestAzureStorageProvider {
+setup_storage()
+test_upload_file()
}
TestLocalStorageProvider ..> LocalStorageProvider : "测试 [backend/open_webui/storage/provider.py:58]"
TestS3StorageProvider ..> S3StorageProvider : "测试 [backend/open_webui/storage/provider.py:101]"
TestS3StorageProvider ..> mock_aws : "使用 [backend/open_webui/test/apps/webui/storage/test_provider.py:100]"
关键测试模式:
- 模拟 AWS:使用
moto的@mock_aws拦截 S3 操作的boto3调用backend/open_webui/test/apps/webui/storage/test_provider.py:100-101。 - 目录隔离:
mock_upload_dir辅助函数通过 monkey-patch 将provider.UPLOAD_DIR替换为临时路径,防止测试污染backend/open_webui/test/apps/webui/storage/test_provider.py:14-19。 - 提供程序发现:
test_get_storage_provider确保get_storage_provider工厂函数正确返回LocalStorageProvider、S3StorageProvider等实例backend/open_webui/test/apps/webui/storage/test_provider.py:31-42。 - 本地提供程序验证:
TestLocalStorageProvider直接在文件系统上验证文件的 CRUD 操作backend/open_webui/test/apps/webui/storage/test_provider.py:59-98。
来源:
backend/open_webui/test/apps/webui/storage/test_provider.py:1-164backend/open_webui/storage/provider.py:40-131
---
前端测试工作流
前端测试分为格式化检查、单元测试和 E2E 测试。
Vitest 单元测试
前端单元测试通过 npm run test:frontend 执行。该过程在 GitHub Actions 中使用 Node.js 22 自动运行 .github/workflows/format-build-frontend.yaml:49-66。
Cypress 端到端测试
Cypress 测试验证完整的应用栈,包括与本地 AI 提供程序(如 Ollama)的集成。
标题:CI/CD 集成测试流水线
graph LR
subgraph "GitHub Action"
checkout["检出代码"]
compose["启动 Docker Compose"]
wait_ollama["等待 Ollama"]
pull_model["拉取测试模型<br/>(qwen:0.5b)"]
run_cypress["运行 Cypress"]
end
subgraph "Docker 栈"
webui_app["open-webui 容器"]
ollama_svc["ollama 容器"]
db_svc["postgres 容器"]
end
compose --> webui_app
compose --> ollama_svc
wait_ollama --> pull_model
pull_model --> run_cypress
run_cypress -.-> webui_app
来源:
.github/workflows/format-build-frontend.yaml:49-66.github/workflows/integration-test.disabled:28-61
---
数据库迁移与重连测试
测试基础设施包括针对多种数据库引擎的数据库 schema 迁移和连接恢复能力的自动化验证。
迁移验证逻辑
集成工作流执行以下步骤:
- SQLite 测试:使用
uvicorn open_webui.main:app启动服务器,并检查/api/config端点是否返回 200 OK 响应.github/workflows/integration-test.disabled:155-173。 - Postgres 测试:使用
DATABASE_URL环境变量,针对真实的 PostgreSQL 实例验证服务器.github/workflows/integration-test.disabled:181-203。 - 重连检查:
- 通过 /health/db 验证初始健康状态 .github/workflows/integration-test.disabled:212。 - 使用执行 pg_terminate_backend 的 Python 脚本强制终止所有 PostgreSQL 后端连接 .github/workflows/integration-test.disabled:219-222。 - 确认应用程序自动重连并返回健康状态 .github/workflows/integration-test.disabled:224-228。
来源:
.github/workflows/integration-test.disabled:105-228
---
测试执行摘要
| 测试类型 | 工具 | 命令 |
|---|---|---|
| 后端单元/存储测试 | pytest | pytest backend/open_webui/test/ |
| 前端单元测试 | Vitest | npm run test:frontend .github/workflows/format-build-frontend.yaml:65 |
| 端到端测试 | Cypress | npx cypress run .github/workflows/integration-test.disabled:53 |
| Linting(后端) | Ruff | ruff format --check . .github/workflows/format-backend.yaml:46 |
| Linting(前端) | Prettier | npm run format .github/workflows/format-build-frontend.yaml:38 |
来源:
.github/workflows/format-backend.yaml:45-46.github/workflows/format-build-frontend.yaml:38,65.github/workflows/integration-test.disabled:53-61