Skip to main content

pytest-datadriver

一个 pytest 插件,支持通过 YAML/JSON 外部数据文件实现数据驱动测试。

特性

  • 多格式支持:YAML 和 JSON 外部数据文件,可扩展架构
  • 零侵入:无需修改现有测试代码,通过 CLI 参数可选激活
  • 参数自动提取:基于 AST 静态分析,从现有测试代码提取 parametrize 参数并导出
  • 双模式覆盖:智能覆盖(按 case_no 匹配)和完全替换模式
  • 两种数据布局:case-first 和 function-first
  • fixture 支持:自动识别 fixture 参数与 parametrize 参数

安装

pip install pytest-datadriver

或从源码安装:

git clone <repo-url>
cd cib-pytest-datadriver
pip install -e .

快速开始

1. 在测试代码中添加标记

import pytest

@pytest.mark.caseNo("TC-001")
@pytest.mark.caseName("加法测试")
@pytest.mark.parametrize("a,b,expected", [
    (1, 2, 3),
    (5, 10, 15),
])
def test_addition(a, b, expected):
    assert a + b == expected

2. 提取参数到数据文件

提取/导出数据文件现已由独立的 datadriver CLI 执行,不再依赖 pytest:

# 基本提取(默认导出为 TestData/data.yaml)
datadriver extract --data-output TestData/data.yaml --test-path tests/

# 导出为 JSON,使用 function-first 布局
datadriver extract --data-output TestData/data.json --data-format json --data-layout func --test-path tests/

# 只导出带 @pytest.mark.caseNo 标记的用例
datadriver extract --data-output TestData/data.yaml --data-export-filter case-only --test-path tests/

3. 使用数据文件运行测试

# 智能覆盖(默认):按 case_no 匹配覆盖参数
pytest tests/ --data-file TestData/data.yaml --data-mode smart

# 完全替换:只执行文件中的用例
pytest tests/ --data-file TestData/data.yaml --data-mode replace

CLI 参数

pytest 运行参数(由插件注册)

参数 说明 可选值 默认值
--data-file 数据文件路径 - 自动查找 TestData/data.yaml
--data-mode 覆盖模式 smart, replace smart
--data-layout 数据布局 case, func case
--data-format 数据格式 yaml, json, auto auto
--data-module-match 模块匹配策略 fullpath, filename fullpath

datadriver extract 独立 CLI 参数

参数 说明 可选值 默认值
--data-output 提取输出路径 - TestData/data.yaml
--data-layout 数据布局 case, func case
--data-format 数据格式 yaml, json, auto auto
--data-export-format 导出详情级别 simple, full simple
--data-export-filter 导出过滤 all, case-only all
--data-module-match 模块匹配策略 fullpath, filename fullpath
--test-path 提取扫描目录 - tests/

数据文件格式

case-first 布局(默认)

TC-001:
  case_name: "加法测试"
  module: "tests/test_math.py"
  function: "test_addition"
  params:
    a: 1
    b: 2
    expected: 3

function-first 布局

"tests/test_math.py::test_addition":
  - case_no: "TC-001"
    case_name: "加法测试"
    params:
      a: 1
      b: 2
      expected: 3

依赖

  • Python >= 3.9
  • pytest >= 7.0
  • pyyaml >= 6.0

打包与发布

本地打包

# 安装构建工具
pip install build twine

# 构建分发包(sdist + wheel)
python -m build

# 构建产物位于 dist/ 目录:
# dist/pytest_datadriver-0.1.0.tar.gz
# dist/pytest_datadriver-0.1.0-py3-none-any.whl

上传到 PyPI

# 上传到 PyPI(需先配置 ~/.pypirc 或使用 Token)
twine upload dist/*

# 上传到 Test PyPI(预发布验证)
twine upload --repository testpypi dist/*

上传到私有制品库

# 上传到私有 PyPI 仓库(需在 ~/.pypirc 中配置仓库地址)
twine upload --repository <your-repo-name> dist/*

# 示例:上传到腾讯云软件源
twine upload --repository-url https://mirrors.tencent.com/repository/pypi/tencent_pypi/simple dist/*

从制品库安装

# 从 PyPI 安装
pip install pytest-datadriver

# 从私有制品库安装
pip install pytest-datadriver -i https://mirrors.tencent.com/repository/pypi/tencent_pypi/simple --trusted-host mirrors.tencent.com

~/.pypirc 配置示例

[distutils]
index-servers =
    pypi
    tencent

[pypi]
username = __token__
password = pypi-xxxxxxxxxxxx

[tencent]
repository = https://mirrors.tencent.com/repository/pypi/tencent_pypi/simple
username = <your-username>
password = <your-password>

许可证

MIT

Metadata

Release files for pytest-datadriver 0.1.3

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

Source distribution (sdist)

Source distribution for pytest-datadriver 0.1.3
File Size Uploaded Provenance
pytest_datadriver-0.1.3.tar.gz 25.4 kB Without provenance Details

Built distribution (wheel)

Table of built distributions (wheels) for pytest-datadriver 0.1.3
File Interpreter ABI Platform
pytest_datadriver-0.1.3-py3-none-any.whl Python 3 none any Details

Total release size: 49.7 kB

Release files / pytest_datadriver-0.1.3.tar.gz

Download URL pytest_datadriver-0.1.3.tar.gz
Size 25.4 kB
Tags Source
SHA-256 checksum
How to use checksums
ddc035a0338673b469b659578566c6ed0b8cb766097d221d682c9278e13262c2
BLAKE2b-256 checksum
How to use checksums
c4b7cef529bdac17cdfe8c23b7e2743de6eb40cf892fe05141440a2785ada4d9
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.14.0

Release files / pytest_datadriver-0.1.3-py3-none-any.whl

Download URL pytest_datadriver-0.1.3-py3-none-any.whl
Size 24.3 kB
Tags Python 3
SHA-256 checksum
How to use checksums
fc66314d9d62ae870b075be4e944ad761c39edf3eb029cca4691c8b88eb77cf7
BLAKE2b-256 checksum
How to use checksums
cbd13011cfd6d5c09aad8aea4ef17c2c8383f1d2b54e78ed235a2d1ee3b10c07
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.14.0

Security for pytest-datadriver 0.1.3

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

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