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 加解密 uv add "df-test-framework[crypto]"
gm SM2 签名/验签与加解密、SM3/HMAC-SM3、SM4 uv add "df-test-framework[gm]"
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 订阅事件)

文档

通过 df-test --help 查看命令入口,初始化项目后参考生成的 README。 完整使用指南、国密说明及版本发布文档由维护团队提供,暂不公开源码仓库地址。


许可证

MIT License

Metadata

Release files for df-test-framework 4.10.2

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.2
File Size Uploaded
df_test_framework-4.10.2.tar.gz 2.8 MB Details

Built distribution (wheel)

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

Total release size: 3.6 MB

Release files / df_test_framework-4.10.2.tar.gz

Download URL df_test_framework-4.10.2.tar.gz
Size 2.8 MB
Tags Source
SHA-256 checksum
How to use checksums
c2d35dd403c650d94b18fa603ddd254744eb5b09f4b401215e90496227b94566
BLAKE2b-256 checksum
How to use checksums
e899120f672d01ff7ae37166f10e03447f9f6fb7cb360eadc917ff407755596e
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.2-py3-none-any.whl

Download URL df_test_framework-4.10.2-py3-none-any.whl
Size 812.3 kB
Tags Python 3
SHA-256 checksum
How to use checksums
aee97621e19a07971d6928dc4dcb1e7fad9084710b93056e8e501373f20177c9
BLAKE2b-256 checksum
How to use checksums
cd2cf26b5938828a474ea71d2b34eaef7980afd1c69fa212834bb7c3650e9aaf
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

This release

4.10.2 This release

2 release files

4.10.1

2 release files

4.10.0

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