[英]pytest: how to explicitly enable a plugin in command line
Let's say I disabled a pytest plugin in my pytest.ini
file like: 假设我在
pytest.ini
文件中禁用了pytest插件,如:
[pytest]
...
addopts=
-p no:myplugin
Now I would like to be able to enable it sometimes with command line arguments, something like: 现在我希望能够使用命令行参数启用它,例如:
pytest -p yes:myplugin
Is that possible? 那可能吗? Please, if you have better recommendations, I would like to know that too.
如果您有更好的建议,我也想知道。
To load the plugin again, use -p pytest_myplugin
. 要再次加载插件,请使用
-p pytest_myplugin
。 This will work when chained after -p no:myplugin
(either on the command-line, or from pytest.ini's addopts). 这在链接到
-p no:myplugin
(在命令行上或从pytest.ini的addopts中链接)都可以使用。
What's happening here: when you specify -p no:plugin
, pytest prepends "pytest_" to "plugin" . 这里发生了什么:当你指定
-p no:plugin
, pytest将“pytest_”添加到“plugin” 。 This is because myplugin
is actually imported from pytest_myplugin
. 这是因为
myplugin
实际上是从pytest_myplugin
导入的。 Unfortunately, this convenience isn't mirrored on the loading side, which requires the full module name of the plugin. 不幸的是,这种便利性没有在加载方面进行镜像,这需要插件的完整模块名称。
I haven't ever needed to do this, as it is easier to disable plugins via commandline flags. 我不需要这样做,因为通过命令行标志禁用插件更容易。 As a workaround you can either specify a different ini file using the
-c
option and either have a different ini file or even use /dev/null
as I have below 作为一种解决方法,您可以使用
-c
选项指定不同的ini文件,并且要么具有不同的ini文件,要么使用/dev/null
,如下所示
$ cat pytest.ini
[pytest]
addopts= -p no:django
$ py.test
================================================= test session starts
platform linux -- Python 3.4.3, pytest-3.0.5, py-1.4.32, pluggy-0.4.0
rootdir: /home/me/python, inifile: pytest.ini
plugins: pep8-1.0.6, cov-2.4.0
collected 0 items
============================================ no tests ran in 0.02 seconds
$ py.test -c /dev/null
================================================= test session starts
platform linux -- Python 3.4.3, pytest-3.0.5, py-1.4.32, pluggy-0.4.0
rootdir: /home/me/python, inifile: /dev/null
plugins: django-3.1.2, pep8-1.0.6, cov-2.4.0
collected 0 items
============================================ no tests ran in 0.02 seconds
If you really need it, you could do something like. 如果你真的需要它,你可以做类似的事情。
py.test -c <(grep -v no:django pytest.ini)
using a unix namedpipe and use grep
or sed
to remove the plugin line. py.test -c <(grep -v no:django pytest.ini)
使用unix py.test -c <(grep -v no:django pytest.ini)
并使用grep
或sed
删除插件行。 But it still seems easier to have all plugins by default and disable via commandline. 但是默认情况下使用所有插件并通过命令行禁用它似乎更容易。
py.test -c <(grep -v no:django pytest.ini)
================================================= test session starts
platform linux -- Python 3.4.3, pytest-3.0.5, py-1.4.32, pluggy-0.4.0
rootdir: /home/me/python, inifile: /dev/fd/63
plugins: django-3.1.2, pep8-1.0.6, cov-2.4.0
collected 0 items
============================================ no tests ran in 0.03 seconds
Alternatively I would not specify addopts= -p no:myplugin
in pytest.ini
and instead use the PYTEST_ADDOPTS
environment variable when I wanted to switch them off. 或者,我不会在
pytest.ini
指定addopts= -p no:myplugin
,而是在我想关闭它时使用PYTEST_ADDOPTS
环境变量。 But this is a slight reverse of what you asked for 但这与你的要求略有相反
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.