pytest:如何在命令行中显式启用插件

eLR*_*uLL 21 python testing pytest

假设我在我的pytest.ini文件中禁用了pytest插件,如:

[pytest]
...
addopts=
    -p no:myplugin
Run Code Online (Sandbox Code Playgroud)

现在我希望能够使用命令行参数启用它,例如:

pytest -p yes:myplugin
Run Code Online (Sandbox Code Playgroud)

那可能吗?如果您有更好的建议,我也想知道.

the*_*man 5

要再次加载插件,请使用-p pytest_myplugin. 这将在链接后起作用-p no:myplugin(无论是在命令行上,还是从 pytest.ini 的 addopts 中)。

这里发生了什么:当您指定时-p no:pluginpytest 将“pytest_”添加到“plugin”前面。这是因为myplugin实际上是从 导入的pytest_myplugin。不幸的是,这种便利并没有反映在加载端,这需要插件的完整模块名称。

  • 至少在 pytest 3.5.1 中这是行不通的。当它解析“-p no:foo”时,该插件将被放置在阻止列表中,任何后续的“-p foo”都无法再重新启用它。请参阅[此处](https://github.com/pytest-dev/pytest/blob/4d0297b4138b694e1d99f4c8147aaf06d725d0f4/src/_pytest/config.py#L422-L429)。 (2认同)