DataKwip功能测试
铁路部署的DataKwip平台的综合功能测试套件。测试所有主要组件,包括API端点、MCP工具、UI自动化和Keycloak认证。
概述
这个测试套件用于验证完整的DataKwip平台栈:
- Keycloak 认证OAuth2 流程、域配置、客户端/用户管理
- DataKwip API(中文可译为“DataKwip应用程序编程接口”)带有OAuth2认证的REST端点,数据库健康状态
- MCP 工具JSON-RPC 2.0 协议,查询实体,获取当前值
- 管理界面(Admin UI)Playwright浏览器自动化,登录流程,导航
先决条件
所需软件
- Python 3.11或更高版本从……下载 python.org
- Playwright 浏览器单独安装(见安装部分)
- Git用于版本控制
铁路服务
所有测试要求以下铁路服务正在运行:
- datakwip认证服务 (Keycloak): (可译为)(关键密钥):
https://datakwip-ai.up.railway.app - datakwip-api 翻译成中文可以是“Datakwip 接口”或“Datakwip API(应用程序编程接口)”,具体取决于上下文和使用场景。这里,“datakwip”可能是一个特定服务或平台的名称,而“-api”则表示这是该服务或平台提供的应用程序编程接口。:
https://datakwip-api-dev.up.railway.app - datakwip-mcp-connector 翻译为中文是:“DataKwip MCP 连接器”:
https://datakwip-mcp-connector-dev.up.railway.app - datakwip-ai-ui 可以翻译为“数据插件-人工智能用户界面”。不过,这里的“datakwip”可能是一个特定产品或项目名称,如果它没有官方的中文译名,通常会保留原英文形式或根据上下文进行适当解释。因此,一个更通用的翻译可能是“(特定数据插件名)- 人工智能用户界面”。如果“datakwip”有特定的中文含义或官方译名,建议使用那个译名。:
https://datakwip-ai-ui-dev.up.railway.app
测试数据
测试假定模拟器已加载测试数据:
- 组织ID:
1 - 在 TimescaleDB 中测试实体和标签
- 在Keycloak中创建的功能测试用户
安装
1. 克隆仓库
cd C:\Users\csper\datakwip-projects\datakwip-platform
# Repository is already in datakwip-functional-tests/2. 创建虚拟环境
cd datakwip-functional-tests
python -m venv .venv3. 激活虚拟环境
Windows(PowerShell):
.venv\Scripts\Activate.ps1Windows(CMD):
.venv\Scripts\activate.batLinux/macOS:
source .venv/bin/activate4. 安装Python依赖项
pip install -e .这会安装:
- pytest(测试框架)
- pytest-asyncio(异步测试支持)
- httpx(HTTP 客户端)
- 网页自动化工具(用于浏览器自动化)
- python-keycloak(Keycloak管理API)
- python-dotenv(环境变量)
- pydantic(配置验证)
5. 安装 Playwright 浏览器
重要这是一个 单独步骤 并且必须手动运行:
playwright install chromium对于其他浏览器:
playwright install firefox
playwright install webkit安装所有浏览器:
playwright install注每个浏览器的安装大小约为300MB。建议使用Chromium进行最快的测试。
配置
1. 复制环境模板
cp .env.example .env2. 编辑 .env 文件
如果与默认值不同,请更新以下值:
# Railway URLs (should match your Railway deployment)
RAILWAY_API_URL=https://datakwip-api-dev.up.railway.app
RAILWAY_MCP_URL=https://datakwip-mcp-connector-dev.up.railway.app
RAILWAY_UI_URL=https://datakwip-ai-ui-dev.up.railway.app
RAILWAY_AUTH_URL=https://datakwip-ai.up.railway.app
# OAuth2 Client (must match Keycloak configuration)
FUNCTIONAL_TESTS_CLIENT_ID=functional-tests
FUNCTIONAL_TESTS_CLIENT_SECRET=functional-tests-secret-2025
# Test User (must exist in Keycloak)
FUNCTIONAL_TEST_USER_EMAIL=functional-test-user@datakwip.local
FUNCTIONAL_TEST_USER_PASSWORD=FunctionalTest2025!
# Keycloak Admin (for admin API tests)
KEYCLOAK_ADMIN=admin
KEYCLOAK_ADMIN_PASSWORD=admin123安全提示永远不要提交(代码/更改等) .env 用于版本控制。其中包含机密信息。
运行测试
运行所有测试
pytest运行特定测试类别
# API tests only
pytest -m api
# MCP tests only
pytest -m mcp
# UI tests only (requires Playwright browsers)
pytest -m ui
# Authentication tests only
pytest -m auth
# Integration tests only
pytest -m integration运行特定测试文件
pytest tests/test_api.py
pytest tests/test_mcp.py
pytest tests/test_ui.py
pytest tests/test_auth.py
pytest tests/test_integration.py运行特定测试功能
pytest tests/test_api.py::test_database_health
pytest tests/test_integration.py::test_full_integration_suite冗长的输出
pytest -v
pytest -vv # Extra verbose显示打印语句
pytest -s并行运行测试
pip install pytest-xdist
pytest -n auto # Use all CPU cores跳过慢速测试
pytest -m "not slow"仅运行集成套件
pytest -m integration -v -s这运行了全面的集成测试,按顺序验证了所有组件。
测试输出
成功的试运行
========== test session starts ==========
platform win32 -- Python 3.11.0
collected 25 items
tests/test_api.py::test_database_health PASSED [ 4%]
tests/test_api.py::test_list_entities PASSED [ 8%]
tests/test_api.py::test_list_entity_tags PASSED [12%]
...
tests/test_integration.py::test_full_integration_suite PASSED [100%]
========== 25 passed in 45.32s ==========测试失败
tests/test_api.py::test_database_health FAILED
FAILED tests/test_api.py::test_database_health - httpx.ConnectError: Connection refused集成套件输出
================================================================================
DATAKWIP PLATFORM FUNCTIONAL TEST SUITE
================================================================================
[1/4] Testing Keycloak Authentication...
✓ Keycloak authentication tests passed (2.34s)
[2/4] Testing API Endpoints...
✓ API endpoint tests passed (1.87s)
- Retrieved 10 entities
- Retrieved 20 tags
[3/4] Testing MCP Tools...
✓ MCP tool tests passed (1.52s)
- Available tools: 5
- Retrieved 10 entities via MCP
[4/4] Testing UI Automation...
✓ UI automation tests passed (8.21s)
- Login: ✓
- Page title: DataKwip Admin
- Data Explorer: Not available
================================================================================
TEST SUITE SUMMARY
================================================================================
✓ PASS Keycloak Auth 2.34s
✓ PASS API Endpoints 1.87s
✓ PASS MCP Tools 1.52s
✓ PASS UI Automation 8.21s
--------------------------------------------------------------------------------
Total execution time: 13.94s
🎉 ALL TESTS PASSED!
================================================================================测试覆盖率
API测试(tests/test_api.py)
- ✅ 数据库健康检查(无需认证)
- ✅ 实体列表,支持OAuth2认证
- ✅ 带有OAuth2认证的实体标签列表
- ✅ OAuth2令牌缓存验证
- API压力测试(10次快速请求)
预期响应时间:
- 数据库健康状态:\_`
示例:
import pytest
@pytest.mark.api
def test_new_endpoint(api_client, config):
"""Test description."""
result = api_client.some_method()
assert result is not None添加新设备
编辑 conftest.py:
@pytest.fixture(scope="session")
def my_fixture(config):
"""Fixture description."""
# Setup
resource = create_resource(config)
yield resource
# Teardown
resource.close()在本地运行测试
你也可以通过更新来测试本地服务 .env:
RAILWAY_API_URL=http://localhost:8000
RAILWAY_MCP_URL=http://localhost:9090
RAILWAY_UI_URL=http://localhost:3000
RAILWAY_AUTH_URL=http://localhost:8081CI/CD 集成
GitHub Actions 示例
name: Functional Tests
on: [push, pull_request]
jobs:
test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: actions/setup-python@v4
with:
python-version: '3.11'
- name: Install dependencies
run: |
pip install -e .
playwright install chromium
- name: Run tests
env:
RAILWAY_API_URL: ${{ secrets.RAILWAY_API_URL }}
FUNCTIONAL_TESTS_CLIENT_SECRET: ${{ secrets.CLIENT_SECRET }}
run: pytest -v最佳实践
- 始终使用 .env 文件不要在代码中硬编码凭证
- 在部署前运行集成套件验证整个平台
- 检查用户界面故障的截图可视化调试极为宝贵
- 使用标记来组织测试在开发过程中运行子集
- 保持测试的幂等性测试不应依赖于顺序或状态
- 使用描述性断言让失败变得易于理解
- 更新 .env.example 文件当添加新配置时
项目结构
datakwip-functional-tests/
├── clients/ # API client implementations
│ ├── __init__.py
│ ├── api_client.py # DataKwip API client (OAuth2)
│ ├── mcp_client.py # MCP JSON-RPC 2.0 client
│ ├── ui_client.py # Playwright automation client
│ └── auth_client.py # Keycloak admin client
├── tests/ # Test modules
│ ├── __init__.py
│ ├── test_api.py # API endpoint tests
│ ├── test_mcp.py # MCP tool tests
│ ├── test_ui.py # UI automation tests
│ ├── test_auth.py # Keycloak auth tests
│ └── test_integration.py # Full integration suite
├── fixtures/ # Test fixtures and helpers
│ └── __init__.py
├── screenshots/ # Playwright screenshots (on failure)
├── conftest.py # Pytest configuration and fixtures
├── pyproject.toml # Python dependencies and config
├── .env.example # Environment variable template
├── .env # Local configuration (DO NOT COMMIT)
├── .gitignore # Git ignore patterns
└── README.md # This file已知的局限性
- Playwright 浏览器安装需要单独的手动安装步骤(约300MB)
- 铁路服务必须正常运行如果任何铁路服务中断,测试将失败
- 测试数据依赖性测试假设模拟器已使用测试数据为组织ID 1进行了初始化
- UI测试脆弱性UI测试依赖于DOM结构,可能会因UI更改而失效
- 网络延迟铁路响应时间可能有所不同;请调整超时设置
.env如需
下一步
功能测试通过后:
- GitHub 仓库主要代理将创建GitHub仓库
- Keycloak 生产环境部署主代理将在铁路系统中部署Keycloak
- 持续集成/持续交付(CI/CD)集成为自动化测试添加GitHub Actions工作流
- 生产测试为生产环境创建单独的测试套件
- 性能测试使用 Locust 或 k6 添加负载测试
- 监测将测试结果与监控仪表板集成
支持
对于问题或疑问:
- 检查铁路服务日志
- 审查Keycloak配置
- 验证测试用户和客户端是否存在
- 检查截图以确认UI测试失败情况
- 使用以下工具运行测试
-v -s用于详细输出
许可证
这个测试套件是DataKwip平台项目的一部分。
______________________________________________________________________
最后更新时间2025年10月21日 版本0.1.0 python3.11及以上版本 Pytest8.0+
