(pytest) pytest-datadir 相关使用说明

发布时间 2023-12-31 20:07:32作者: bruce_he

pytest-datadir 是 pytest 第三方插件,用于测试数据的管理。

官方文档说明链接:https://pypi.org/project/pytest-datadir/

 

step1: 安装 

pip install pytest-datadir -i https://pypi.tuna.tsinghua.edu.cn/simple

 

step2: 示例程序:

"""
目录结构:
project
├── test/
│   └── data/
│       └── userinfo.csv
│   └── test_pytest_datadir/
│       └── userinfo_local.csv
└── test_pytest_datadir.py
"""


def test_share_datadir(shared_datadir):    # 第一种方式
    """
    :param shared_datadir: => 固定名称,数据存储在 /test/data 目录下
    :return:
    """
    file_path = shared_datadir / "userinfo.csv"
    with open(file_path, "r", encoding='utf-8') as f:
        content = f.read()
        assert "username" in content


def test_datadir(datadir):    # 第二种方式
    """
    :param datadir: => 固定名称,数据存储目录必须在 /test/调用该方法的.py文件名(module name)
    :return:
    """
    file_path = datadir / "userinfo_local.csv"
    with open(file_path, "r", encoding='utf-8') as f:
        content = f.read()
        assert "username" in content