代码之家  ›  专栏  ›  技术社区  ›  thinwybk

当使用tox运行时,如何使pytest测试可以访问测试数据文件?

  •  2
  • thinwybk  · 技术社区  · 7 年前

    我想经营一家公司 test Path 通过参数输入到文件: function(some_path_to_file) 通过 tox pytest s builtin tmpdir fixtures )但它存在于包中 <package>/data 测试目录之外的目录 <package>/tests (位置) <package>/tests/data 毒物 在virtualenv中运行测试我不清楚如何使测试数据文件对测试可用。我知道我可以定义 --basedir option 但我没有让它与我一起工作 然而

    问题是一种转换 some_path_to_file 从…起 路径 str (传给 sqlite3.connect(database 内部 function Path.resolve( --巴塞迪尔 选项和 毒物

    2 回复  |  直到 7 年前
        1
  •  3
  •   Lina    7 年前

    这也把我绊倒了。我能解决的办法是 指定完整路径 我希望测试函数读取的文本文件的 相对的 到基本目录。

    例如,我的目录树如下所示:

    .
    ├── __init__.py
    ├── my_package
    │   ├── __init__.py
    │   └── calculate_stats.py
    ├── my_package.egg-info
    │   ├── PKG-INFO
    │   ├── SOURCES.txt
    │   ├── dependency_links.txt
    │   ├── requires.txt
    │   └── top_level.txt
    ├── bin
    │   └── calculate_stats
    ├── requirements
    │   ├── default.txt
    │   └── development.txt
    ├── setup.py
    ├── test
    │   ├── __init__.py
    │   ├── test_calculate_stats.csv
    │   ├── test_calculate_stats.txt
    │   └── test_calculate_stats.py
    └── tox.ini
    

    test_calculate_stats.py

    assert (calculate_stats.calculate_stats_to_csv("test/test_calculate_stats.txt", "test/test_calculate_stats.csv") == 60)
    

    这个 calculate_stats_to_csv 函数读入 test/test_calculate_stats.txt 文件,计算一些统计信息,并将它们输出到 test/test_calculate_stats.csv

    test_calculate_stats.txt 因为它与包含测试函数的文件位于同一个目录中——这就是我遇到错误的时候。

        2
  •  0
  •   phd    7 年前

    tox 预先罚款若干 substitutions {envdir} , site-packages {envsitepackagesdir} . 将值从命令行传递到测试脚本,如下所示:

    [testenv]
        commands = pytest --basedir={envsitepackagesdir}/mypackage