A pytest plugin for data-driven testing with YAML/JSON external data files
Project description
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. 提取参数到数据文件
pytest --data-extract --data-output TestData/data.yaml --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 参数
| 参数 | 说明 | 可选值 | 默认值 |
|---|---|---|---|
--data-file |
数据文件路径 | - | 自动查找 TestData/data.yaml |
--data-mode |
覆盖模式 | smart, replace | smart |
--data-layout |
数据布局 | case, func | case |
--data-format |
数据格式 | yaml, json, auto | auto |
--data-extract |
触发提取模式 | - | - |
--data-output |
提取输出路径 | - | TestData/data.yaml |
--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
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
pytest_datadriver-0.1.1.tar.gz
(23.0 kB
view details)
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file pytest_datadriver-0.1.1.tar.gz.
File metadata
- Download URL: pytest_datadriver-0.1.1.tar.gz
- Upload date:
- Size: 23.0 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.14.0
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
15cc23d658773070c4d26f93d6883083129385508cda2cc7f424227dd2737a56
|
|
| MD5 |
0a92e46d63d6ef0555fbdae00312d215
|
|
| BLAKE2b-256 |
c29c351616f52149f8529dbcc5c7b7d92e198b205c87076234c0a902ea51fc01
|
File details
Details for the file pytest_datadriver-0.1.1-py3-none-any.whl.
File metadata
- Download URL: pytest_datadriver-0.1.1-py3-none-any.whl
- Upload date:
- Size: 22.2 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.14.0
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
a0ca4a1f6af87341be77553585aa9cf62abfa1d4b75e81d3c1426fa5d53f1949
|
|
| MD5 |
b72c0a6ba05048c091a20ff4a5e008f4
|
|
| BLAKE2b-256 |
5793d11474ee27124046686e388335b24a0fa8aa7bcd8051e8ab3f70d5f12885
|