随笔 - 148  文章 - 3  评论 - 2  阅读 - 11万

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

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

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

 

step1: 安装 

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

 

step2: 示例程序:

复制代码
"""
目录结构:
project or package
├── 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
复制代码

 

posted on   bruce_he  阅读(90)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 分享4款.NET开源、免费、实用的商城系统
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
· 上周热点回顾(2.24-3.2)
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

点击右上角即可分享
微信分享提示