python data_error ai_generated true

FileNotFoundError: [Errno 2] 没有那个文件或目录: '/repo/tests/test_parser/input.csv'

FileNotFoundError: [Errno 2] No such file or directory: '/repo/tests/test_parser/input.csv'

ID: python/pytest-datadir-missing-file

其他格式: JSON · Markdown 中文 · English
80%修复率
80%置信度
0证据数
2024-06-01首次发现

版本兼容性

版本状态引入弃用备注
0.8 active — — —

根因分析

pytest-datadir 将 `data/` 解析为相对于测试模块的目录;文件路径错误、插件未安装,或测试文件移动时未一并移动数据目录。

English

pytest-datadir resolves `data/` relative to the test module's directory; the file lives in the wrong path, the plugin is not installed, or the test file was moved without moving its data directory.

generic

解决方案

  1. 90% 成功率
    Place data next to the test as `tests/test_parser/input.csv` and reference it via the `datadir` fixture:
    def test_parse(datadir):
        path = datadir / 'input.csv'
        assert path.exists()
  2. 85% 成功率
    Or use `tmp_path` + `shutil.copy` from a committed fixtures dir:
    import shutil
    FIXTURES = Path(__file__).parent / 'fixtures'
    def test_parse(tmp_path):
        src = FIXTURES / 'input.csv'
        dst = tmp_path / 'input.csv'
        shutil.copy(src, dst)

无效尝试

常见但无效的做法:

  1. 90% 失败

    Breaks on CI and other machines; path does not exist there.

  2. 75% 失败

    Silently produces empty fixtures and the test asserts against wrong data.