【问题标题】:pytest How to locate input file for conftest.py in test directory when run test from parent directorypytest 从父目录运行测试时如何在测试目录中找到 conftest.py 的输入文件
【发布时间】:2012-07-29 20:29:59
【问题描述】:

在tests目录中有两个子目录,例如tests/api和tests/sanity。注意 tests/api 有 conftest.py 期望在 tests/api 目录中找到 input.conf。请注意,从 tests/api 运行测试可以正常工作。但是从父目录测试运行测试,它失败了: py.test --test_option=input.conf 用法:py.test [options] [file_or_dir] [file_or_dir] [...]

py.test:错误:没有这样的选项:--test_option

有人可以展示我如何构建我的测试,以便它可以工作吗? 我也尝试指定测试目录,如下所示,但找不到 input.conf。 py.test --test_option=input.conf api/test_api.py

E IOError: [Errno 2] 没有这样的文件或目录:'input.conf'

【问题讨论】:

    标签: pytest


    【解决方案1】:

    假设您的语法是正确的(我认为您是转置文件的名称和扩展名),您只需要正确指定文件的位置,包括相对或绝对路径:

    代替:

    py.test --test_option=input.conf
    

    做:

    py.test --test_option=api/input.conf 
    

    【讨论】:

    • 太棒了。这行得通。 py.test --test_option=api/input.conf api/test_api.py 。一个后续问题是我是否有多个包含测试的目录,并且每个目录都有自己的 input.conf 文件,我想运行所有这些。这可能吗?
    • 是的,但是您需要稍微更改一下程序。看看这些文件/目录函数docs.python.org/library/os.html#files-and-directories,如果您对它感到满意,请接受答案(或者问尽可能多的 Python 问题 :-))
    猜你喜欢
    • 1970-01-01
    • 2018-10-14
    • 1970-01-01
    • 2021-06-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-12-09
    相关资源
    最近更新 更多