pytest 找不到文件?直接在 pytest.ini 配置根目录 + 路径(最简单方案)

在 pytest.ini 里配置项目根目录 / 路径,让 pytest 执行用例时永远能找到文件 ,pytest 专用、最标准、最省事的配置方法,不用自己写复杂代码!

一、核心结论

✅ pytest.ini 可以配置项目根目录、测试数据目录、报告目录等所有路径 ✅ 配置后,无论在哪执行 pytest,都不会报 FileNotFound✅ 不用写死绝对路径,自动适配所有电脑


二、最简可用:pytest.ini 完整配置

直接在你项目根目录 创建 / 修改 pytest.ini:

ini

复制代码
[pytest]
# 1. 配置项目根目录(自动识别,不用改)
pythonpath = .
# 2. 自定义你的测试文件根路径(可写多个)
test_data_path = data
report_path = reports
case_path = test_cases

# 其他 pytest 基础配置(可保留)
addopts = -v
testpaths = test_cases

这三行就是关键!

ini

复制代码
pythonpath = .                # 把项目根目录加入 Python 路径
test_data_path = data         # 测试数据文件夹
report_path = reports         # 报告文件夹
case_path = test_cases        # 用例文件夹

三、Python /pytest 中读取 pytest.ini 里的路径

直接用pytest 内置配置读取,任何测试用例里都能用:

python

运行

复制代码
import pytest
import os

# 读取 pytest.ini 中的路径
def test_demo():
    # 1. 获取项目根目录
    root_dir = os.getcwd()  # 因为配置了 pythonpath = . 所以永远正确
    
    # 2. 读取 pytest.ini 里自定义的路径
    data_dir = pytest.config.getini("test_data_path")
    report_dir = pytest.config.getini("report_path")

    # 3. 拼接绝对路径(永远不会找不到文件)
    data_file = os.path.join(root_dir, data_dir, "test.xlsx")

    print("文件路径:", data_file)

用法示例(再也不会错)

python

运行

复制代码
# 读取 data 目录下的文件 → 100% 找到
file = os.path.join(os.getcwd(), pytest.config.getini("test_data_path"), "user.json")
with open(file) as f:
    data = f.read()

四、最关键的一行(解决 99% 路径错误)

ini

复制代码
[pytest]
pythonpath = .

作用: 把项目根目录 加入 Python 搜索路径无论你在哪个子目录执行 pytest 命令os.getcwd() 永远 = 项目根目录所有相对路径直接生效


五、你的项目结构推荐(标准结构)

plaintext

复制代码
你的项目/
├─ pytest.ini       # 配置文件
├─ test_cases/      # 用例
├─ data/            # 测试数据
└─ reports/         # 报告

配置后执行:

bash

运行

复制代码
pytest

所有文件都能找到!


六、终极万能方案(复制即用)

1. pytest.ini

ini

复制代码
[pytest]
pythonpath = .
test_data = data
report = reports
testpaths = test_cases
addopts = -v --alluredir=reports

2. conftest.py(pytest 全局文件)

python

运行

复制代码
import os
import pytest

@pytest.fixture(scope="session")
def project_root():
    """项目根目录"""
    return os.getcwd()

@pytest.fixture(scope="session")
def data_path(project_root):
    """测试数据路径"""
    data_dir = pytest.config.getini("test_data")
    return os.path.join(project_root, data_dir)

3. 测试用例中使用

python

运行

复制代码
def test_read_file(data_path):
    file_path = os.path.join(data_path, "test.json")
    with open(file_path, "r") as f:
        data = f.read()
    assert data is not None

总结(你只需要记住 2 步)

  1. pytest.ini 加一行:pythonpath = .
  2. 用 os.getcwd() 获取项目根目录
  3. 所有文件路径 = 根目录 + 相对路径(永不报错)
相关推荐
happylifetree9 小时前
Python15:核心语法-数据存储与运算-字符串拼接
python
Sarvartha9 小时前
对象创建与引用
java·开发语言
hasty9 小时前
限制写了却没生效:OpenTelemetry Go 的 Unicode 截断边界
开发语言·后端·golang
huisheng_qaq9 小时前
【Python基础篇-09】深入理解python的闭包与装饰器
python·闭包
编码者卢布10 小时前
【App Service 】WebJobs 多实例实验:谁在运行,什么时候运行?
开发语言·python
kaixin_learn_qt_ing10 小时前
Qt抽屉窗体实现demo
开发语言·qt
quantdash_cc10 小时前
Python 获取实时行情后如何进行批量筛选?从全市场快照到策略候选池
开发语言·python·数据分析·量化交易·股票数据·quantdash
论文复现现场10 小时前
论文复现看到“RTX 3090 or higher”:显存、CUDA、batch size 与 OOM 怎么判断?
开发语言·pytorch·batch·cuda·rtx3090
智鸟科技GemeOpen开发者智能设备10 小时前
GemeOpen 智能音箱 GSSM0B - 播放控制(Java示例)
java·开发语言·智能音箱
小蜗 strong11 小时前
和电脑猜拳(随机程序应用)
服务器·前端·python