配置

命令行选项和配置文件设置

通过使用常规帮助选项,可以获得有关ini样式配置文件中命令行选项和值的帮助:

pytest -h   # prints options _and_ config file settings

这将显示已安装插件注册的命令行和配置文件设置。

配置文件格式

很多 pytest settings 可以设置在 配置文件 ,它通常位于存储库的根目录或测试文件夹中。

pytest支持的配置文件的快速示例:

pytest.ini

pytest.ini 文件优先于其他文件,即使是空的。

# pytest.ini
[pytest]
minversion = 6.0
addopts = -ra -q
testpaths =
    tests
    integration

pyproject.toml

6.0 新版功能.

pyproject.toml are considered for configuration when they contain a tool.pytest.ini_options table.

# pyproject.toml
[tool.pytest.ini_options]
minversion = "6.0"
addopts = "-ra -q"
testpaths = [
    "tests",
    "integration",
]

注解

人们可能会想知道为什么 [tool.pytest.ini_options] 而不是 [tool.pytest] 和其他工具一样。

原因是pytest团队打算在将来充分利用富TOML数据格式进行配置,保留 [tool.pytest] 那张桌子。这个 ini_options 目前,表被用作连接现有 .ini 配置系统和未来的配置格式。

tox.ini

tox.ini files are the configuration files of the tox 项目,并且还可以用于保存pytest配置,如果它们具有 [pytest] 部分。

# tox.ini
[pytest]
minversion = 6.0
addopts = -ra -q
testpaths =
    tests
    integration

setup.cfg

setup.cfg files are general purpose configuration files, used originally by distutils ,并且还可以用于保存pytest配置,如果它们具有 [tool:pytest] 部分。

# setup.cfg
[tool:pytest]
minversion = 6.0
addopts = -ra -q
testpaths =
    tests
    integration

警告

用法 setup.cfg 除非用于非常简单的用例,否则不推荐使用。 .cfg 文件使用不同于 pytest.initox.ini 这可能会导致难以追踪的问题。如果可能,建议使用后一个文件,或者 pyproject.toml ,以保存pytest配置。

初始化:确定rootdir和configfile

Pytest确定 rootdir 对于依赖于命令行参数(指定的测试文件、路径)和配置文件是否存在的每个测试运行。坚定的 rootdirconfigfile 在启动期间作为pytest头的一部分打印。

这里有一个总结什么 pytest 使用 rootdir 为:

  • 构建 诺迪斯 在收集过程中,每个测试都被分配一个唯一的 诺迪德 根植于 rootdir 并考虑完整路径、类名、函数名和参数化(如果有)。

  • 被插件用作存储项目/测试运行特定信息的稳定位置;例如,内部 cache 插件创建一个 .pytest_cache 子目录 rootdir 存储其交叉测试运行状态。

rootdir is NOT used to modify sys.path/PYTHONPATH 或者影响模块的导入方式。见 Pytest导入机制和 sys.path/PYTHONPATH 了解更多详细信息。

这个 --rootdir=path 命令行选项可用于强制指定目录。请注意,与其他命令行选项相反, --rootdir 不能与一起使用 addopts 里面 pytest.ini 因为 rootdir 习惯于 find pytest.ini 已经。

寻找 rootdir

下面是从中查找rootdir的算法 args

  • 为指定的 args 它们被识别为文件系统中存在的路径。如果找不到这样的路径,则将公共祖先目录设置为当前工作目录。

  • 寻找 pytest.inipyproject.tomltox.inisetup.cfg 祖先目录及以上目录中的文件。如果一个匹配,它将成为 configfile 它的目录变成 rootdir .

  • 如果未找到配置文件,请查找 setup.py 从公共祖先目录向上确定 rootdir .

  • 如果没有 setup.py 找到了,寻找 pytest.inipyproject.tomltox.inisetup.cfg 在每个指定的 args 向上。如果一个匹配,它将成为 configfile 它的目录变成 rootdir .

  • 如果没有 configfile 已找到,请使用已确定的共同祖先作为根目录。这允许在不属于包的结构中使用pytest,并且没有任何特定的配置文件。

如果没有 args 如果给定,pytest将在当前工作目录下收集测试,并开始确定 rootdir 从那里。

只有在以下情况下,才会匹配配置文件:

  • pytest.ini :将始终匹配并优先,即使为空。

  • pyproject.toml: contains a [tool.pytest.ini_options] table.

  • tox.ini :包含 [pytest] 部分。

  • setup.cfg :包含 [tool:pytest] 部分。

文件按上述顺序考虑。多个选项 configfiles 候选人永远不会合并-第一场比赛获胜。

内部 Config 对象(可通过挂钩或 pytestconfig fixture)随后将具有以下属性:

  • config.rootpath :确定的根目录,保证存在。

  • config.inipath :已决定 configfile ,可能是 None (它被命名为 inipath 因为历史原因)。

6.1 新版功能: 这个 config.rootpathconfig.inipath 属性。他们是 pathlib.Path 旧版本 config.rootdirconfig.inifile ,有类型 py.path.local ,并且仍然存在,以便向后兼容。

这个 rootdir 用作构造测试地址(“nodeid”)的参考目录,也可由插件使用以存储每次测试运行的信息。

例子:

pytest path/to/testdir path/other/

将共同祖先确定为 path 然后检查配置文件,如下所示:

# first look for pytest.ini files
path/pytest.ini
path/pyproject.toml  # must contain a [tool.pytest.ini_options] table to match
path/tox.ini         # must contain [pytest] section to match
path/setup.cfg       # must contain [tool:pytest] section to match
pytest.ini
... # all the way up to the root

# now look for setup.py
path/setup.py
setup.py
... # all the way up to the root

警告

自定义pytest插件命令行参数可以包含路径,如 pytest --log-output ../../test.log args. Then args is mandatory, otherwise pytest uses the folder of test.log for rootdir determination (see also issue 1435). A dot . 也可以引用当前工作目录。

内置配置文件选项

有关完整的选项列表,请参阅 reference documentation .