Skip to main content

DF Test Framework

企业级 Python 测试自动化框架,支持 HTTP/UI/数据库/消息队列多场景测试

PyPI version Python License


核心特性

  • 多协议客户端 — HTTP/GraphQL/gRPC,同步/异步双模式,中间件拦截器链(重试、签名、Bearer Token)
  • 异步高性能 — AsyncHttpClient 并发性能提升 10-30 倍,AsyncDatabase/AsyncRedis/异步 MQ 完整双轨支持
  • UI 自动化 — Playwright,同步/异步,浏览器会话复用,失败自动截图 + trace 取证
  • 数据库访问 — SQLAlchemy 2.0,多方言(MySQL/PostgreSQL/SQLite/MSSQL),Repository + Unit of Work 模式
  • 测试数据管线 — CaseContext[T] + YAML 驱动参数化,支持 skip/xfail(strict)/seed/tags,CLI 精准过滤复现
  • 响应契约 — http.response_contract 配置驱动业务错误检查,无需自定义基类;expect_business_error 断言
  • 命名与清理 — NamingStandard 命名协议 + df-test cleanup 兜底残留清理(SQL/Redis 双载体)
  • 基准数据池 — BaselinePool + df-test baseline check|ensure,共享环境受管数据幂等保障与巡检
  • 闸门与竞态 — requires_env/requires_flag 统一 skip、wait_until/assert_consistently 轮询原语、race() 竞态栅栏
  • 多客户端模型 — @service_client / @ui_client / http_service() 零子类声明,多服务/多前端各自独立
  • CLI 脚手架 — df-test init 一键生成 api/ui/full 最小骨架,df-test doctor 项目体检
  • 可观测性 — 语义事件驱动:OpenTelemetry 链路追踪 + Prometheus 监控 + Allure 报告自动集成
  • 消息队列 — Kafka/RabbitMQ/RocketMQ 统一接口
  • 存储客户端 — 本地文件 / AWS S3 / 阿里云 OSS
  • 脱敏服务 — HTTP/UI/DB 日志中敏感字段自动遮盖,集中配置
  • Mock 工具 — HTTP/数据库/Redis/时间 Mock + Playwright 路由 Mock(page_mock 轮换/延迟应答),开箱即用

安装

# 使用 uv(推荐)
uv add df-test-framework

# 使用 pip
pip install df-test-framework

可选依赖(按需安装,核心功能无需额外依赖):

依赖组 功能 安装命令
ui Playwright UI 测试 uv add "df-test-framework[ui]"
database-async 异步数据库驱动(MySQL/PG/SQLite) uv add "df-test-framework[database-async]"
database-mssql MSSQL 驱动 uv add "df-test-framework[database-mssql]"
mq 消息队列(Kafka + RabbitMQ + RocketMQ) uv add "df-test-framework[mq]"
observability OpenTelemetry + Prometheus uv add "df-test-framework[observability]"
storage 对象存储(S3 + 阿里云 OSS) uv add "df-test-framework[storage]"
crypto AES/DES 加解密 uv add "df-test-framework[crypto]"
all 所有可选功能 uv add "df-test-framework[all]"

快速开始

脚手架建项目(推荐)

df-test init my-project              # API 测试项目
df-test init my-project --type ui    # UI 测试项目
df-test init my-project --type full  # API + UI 混合

cd my-project && pytest -v

API 测试

from df_test_framework import api_class, BaseAPI
from pydantic import BaseModel

class User(BaseModel):
    id: int
    name: str

@api_class()
class UserAPI(BaseAPI):
    def get_user(self, user_id: int) -> User:
        # 便捷方法返回解析后的数据:传 model= 得 Pydantic 模型,不传得 dict;
        # HTTP 4xx/5xx 自动抛 HttpError,配置 http.response_contract 后业务失败自动抛 BusinessError
        return self.get(f"/users/{user_id}", model=User)

def test_get_user(user_api: UserAPI):   # fixture 由 @api_class 自动注册
    user = user_api.get_user(1)
    assert user.id == 1

异步并发测试

import asyncio
from df_test_framework import AsyncHttpClient

async def test_concurrent():
    async with AsyncHttpClient("https://api.example.com") as client:
        tasks = [client.get(f"/users/{i}") for i in range(100)]
        responses = await asyncio.gather(*tasks)
        assert all(r.status_code == 200 for r in responses)

YAML 驱动参数化

# tests/data/login.yaml
tests:
  valid_user:
    title: "正常登录"
    marks: [smoke]
    data:
      username: admin
      password: secret
  locked_user:
    title: "锁定账号登录失败"
    xfail: "已知 Bug #123"
    data:
      username: locked
      password: secret
import pytest
from df_test_framework.testing.data.pipeline import case_dataset, CaseContext

@pytest.mark.parametrize("ctx", case_dataset("tests/data/login.yaml"))
def test_login(ctx: CaseContext, login_api):
    result = login_api.login(ctx.data["username"], ctx.data["password"])
    assert result["token"]

架构

Layer 4 ─── bootstrap/          # 引导层:Bootstrap、Providers、RuntimeContext
Layer 3 ─── testing/ + cli/     # 门面层:pytest Fixtures、CLI 脚手架
Layer 2 ─── capabilities/       # 能力层:HTTP/UI/DB/MQ/Storage
Layer 1 ─── infrastructure/     # 基础设施:config/logging/events/plugins
Layer 0 ─── core/               # 核心层:纯抽象(无第三方依赖)
横切  ───── plugins/             # 插件:MonitoringPlugin(Allure 走 Fixture 订阅事件)

文档

完整文档见项目 GitHub 仓库:


许可证

MIT License

Metadata

Release files for df-test-framework 4.10.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for df-test-framework 4.10.0
File Size Uploaded
df_test_framework-4.10.0.tar.gz 2.8 MB Details

Built distribution (wheel)

Table of built distributions (wheels) for df-test-framework 4.10.0
File Interpreter ABI Platform
df_test_framework-4.10.0-py3-none-any.whl Python 3 none any Details

Total release size: 3.6 MB

Release files / df_test_framework-4.10.0.tar.gz

Download URL df_test_framework-4.10.0.tar.gz
Size 2.8 MB
Tags Source
SHA-256 checksum
How to use checksums
b8cc4446bcbf94ff0ef311bfca84011589da82f833e799307f1be35a8aa42777
BLAKE2b-256 checksum
How to use checksums
d78f2364ce44653c2c98a51de5c092bea6e57e56491bde43c89c49481bb13a89
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.13.14

Release files / df_test_framework-4.10.0-py3-none-any.whl

Download URL df_test_framework-4.10.0-py3-none-any.whl
Size 811.2 kB
Tags Python 3
SHA-256 checksum
How to use checksums
cdbb12a2cec15df69a969bcd2870062b8c64b47f9acb88b48fbda98cf6eb55a7
BLAKE2b-256 checksum
How to use checksums
32ab8815a35e90d22fc24fe6763d7a334dfb519d398780a14ab36afbfbed11ef
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.13.14

Release history Release notifications | RSS feed

4.10.2

2 release files

4.10.1

2 release files

This release

4.10.0 This release

2 release files

4.9.1

2 release files

4.9.0

2 release files

4.8.1

2 release files

4.8.0

2 release files

4.7.5

2 release files

4.7.4

2 release files

4.7.3

2 release files

4.7.2

2 release files

4.7.1

2 release files

4.7.0

2 release files

4.5.0

2 release files

4.4.4

2 release files

4.4.3

2 release files

4.4.2

2 release files

4.3.1

2 release files

4.3.0

2 release files

4.2.2

2 release files

4.2.1

2 release files

4.1.2

2 release files

4.1.1

2 release files

4.1.0

2 release files

4.0.3

2 release files

4.0.2

2 release files

4.0.1

2 release files

4.0.0

2 release files

3.46.3

2 release files

3.46.2

2 release files

3.38.1

2 release files

3.37.0

2 release files

3.35.5

2 release files

3.28.1

2 release files

3.11.0

2 release files

3.10.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page