如何禁用 pytest.ini 中的多个插件? [英] How to disable multiple plugins in pytest.ini?
问题描述
我在同一个存储库(单独的 pytest.ini 文件)中有需要不同 pytest 插件的测试.如何在 pytest.ini 中禁用多个插件而不卸载它们?
I have tests within the same repository (separate pytest.ini files) that require different pytest plugins. How can I disable multiple plugins in pytest.ini without uninstalling them?
https://docs.pytest.org/en/latest/plugins.html#findpluginname
addopts = --nomigrations --reuse-db -s -p no:pytest-splinter
工作正常,但我还想为其中一个测试套件禁用 pytest-django 和 pytest-bdd.我怎样才能做到这一点?我试过了:
works fine, but I also want to disable pytest-django and pytest-bdd for one of the test suites. How can I do that? I've tried:
addopts = --nomigrations --reuse-db -s -p no:pytest-splinter -p no:pytest-django
addopts = --nomigrations --reuse-db -s -p no:pytest-splinter no:pytest-django
addopts = --nomigrations --reuse-db -s -p no:pytest-splinter pytest-django
全部失败,并且文档没有描述这是如何完成的.非常感谢任何指点,谢谢!
all fail, and the documentation doesn't describe how this is done. Any pointers greatly appreciated, thanks!
推荐答案
重复传递 -p
选项的用法是正确的.但是,您使用了错误的插件名称.使用 pytest
插件名称,而不是传递 PyPI 包名称:
The usage with the repeated passing of the -p
option is the correct one. However, you are using the wrong plugin names. Instead of passing the PyPI package names, use the pytest
plugin names:
addopts = --nomigrations --reuse-db -s -p no:pytest-splinter -p no:django
如果不确定是否使用了正确的插件名称,请使用 pytest --trace-config
列出所有已安装的插件及其名称:
When in doubt whether using correct plugin name, use pytest --trace-config
to list all installed plugins along with their names:
$ pytest --trace-config
...
PLUGIN registered: <module 'pytest_html.plugin' from '/Users/hoefling/.virtualenvs/stackoverflow/lib/python3.6/site-packages/pytest_html/plugin.py'>
PLUGIN registered: <module 'pytest_django.plugin' from '/Users/hoefling/.virtualenvs/stackoverflow/lib/python3.6/site-packages/pytest_django/plugin.py'>
...
=============================================== test session starts ===============================================
platform darwin -- Python 3.6.4, pytest-3.7.3.dev26+g7f6c2888, py-1.5.4, pluggy-0.7.1
using: pytest-3.7.3.dev26+g7f6c2888 pylib-1.5.4
setuptools registered plugins:
pytest-metadata-1.7.0 at /Users/hoefling/.virtualenvs/stackoverflow/lib/python3.6/site-packages/pytest_metadata/plugin.py
pytest-html-1.19.0 at /Users/hoefling/.virtualenvs/stackoverflow/lib/python3.6/site-packages/pytest_html/plugin.py
pytest-django-3.4.2 at /Users/hoefling/.virtualenvs/stackoverflow/lib/python3.6/site-packages/pytest_django/plugin.py
active plugins:
metadata : /Users/hoefling/.virtualenvs/stackoverflow/lib/python3.6/site-packages/pytest_metadata/plugin.py
html : /Users/hoefling/.virtualenvs/stackoverflow/lib/python3.6/site-packages/pytest_html/plugin.py
django : /Users/hoefling/.virtualenvs/stackoverflow/lib/python3.6/site-packages/pytest_django/plugin.py
...
当 pytest --trace-config
失败时
在这种情况下,您可以直接查询已安装包的元数据,例如使用 pkg_resources
(setuptools
包的一部分,它预装在大多数 Python现在的发行版;如果没有,照常安装:pip install --user setuptools
):
When pytest --trace-config
fails
In that case, you could query the metadata of the installed packages directly, for example using pkg_resources
(part of setuptools
package, which is preinstalled on most of the Python distributions nowadays; if not, install as usual: pip install --user setuptools
):
import os
import pkg_resources
data = ['{}-{}: {}'.format(dist.project_name, dist.version,
' '.join(dist.get_entry_map(group='pytest11').keys()))
for dist in pkg_resources.working_set if dist.get_entry_map(group='pytest11')]
print(os.linesep.join(data))
示例输出:
requests-mock-1.5.2: requests_mock
pytest-splinter-1.9.1: pytest-splinter
pytest-metadata-1.7.0: metadata
pytest-html-1.19.0: html
pytest-django-3.4.2: django
找出插件名称的另一种可能是查看插件的源代码.该名称在插件的入口点声明中:
Another possibility to find out the plugin name is to look in the plugin's source code. The name is in the plugin's entry point declaration:
entry_points={'pytest11': [
'plugin_name=plugin.registration.module',
]}
因此,
这篇关于如何禁用 pytest.ini 中的多个插件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!