如何禁用 pytest.ini 中的多个插件?

Liz*_*Liz 5 python testing pytest

我在同一个存储库(单独的 pytest.ini 文件)中有需要不同 pytest 插件的测试。如何在不卸载的情况下禁用 pytest.ini 中的多个插件?

https://docs.pytest.org/en/latest/plugins.html#findpluginname

addopts = --nomigrations --reuse-db -s -p no:pytest-splinter 
Run Code Online (Sandbox Code Playgroud)

工作正常,但我也想为其中一个测试套件禁用 pytest-django 和 pytest-bdd。我怎样才能做到这一点?我试过了:

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 
Run Code Online (Sandbox Code Playgroud)

都失败了,并且文档没有描述这是如何完成的。任何指针都非常感谢,谢谢!

hoe*_*ing 9

重复传递选项的用法-p是正确的。但是,您使用了错误的插件名称。不要传递 PyPI 包名称,而是使用pytest插件名称:

addopts = --nomigrations --reuse-db -s -p no:pytest-splinter -p no:django
Run Code Online (Sandbox Code Playgroud)

当不确定是否使用正确的插件名称时,请使用pytest --trace-config列出所有已安装的插件及其名称:

$ 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
...
Run Code Online (Sandbox Code Playgroud)

pytest --trace-config失败时

在这种情况下,您可以直接查询已安装包的元数据,例如使用pkg_resources(package 的一部分setuptools,现在大多数 Python 发行版都预装了;如果没有,请照常安装: 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))
Run Code Online (Sandbox Code Playgroud)

输出示例:

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
Run Code Online (Sandbox Code Playgroud)

查找插件名称的另一种可能性是查看插件的源代码。该名称位于插件的入口点声明中:

entry_points={'pytest11': [
    'plugin_name=plugin.registration.module',
]}
Run Code Online (Sandbox Code Playgroud)

因此,