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

时间:2018-08-28 02:36:09

标签: python testing pytest

我在需要不同pytest插件的同一存储库(单独的pytest.ini文件)中进行了测试。如何在pytest.ini中禁用多个插件而不卸载它们?

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

addopts = --nomigrations --reuse-db -s -p no:pytest-splinter 

可以正常工作,但是我也想为其中一个测试套件禁用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 

全部失败,并且文档没有描述如何完成此操作。任何指针都非常感谢,谢谢!

1 个答案:

答案 0 :(得分:3)

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

addopts = --nomigrations --reuse-db -s -p no:pytest-splinter -p no:django

如果不确定是否使用正确的插件名称,请使用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
...

pytest --trace-config失败时

在这种情况下,您可以直接查询已安装软件包的元数据,例如使用pkg_resourcessetuptools软件包的一部分,该软件包已预先安装在当今的大多数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))

示例输出:

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

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

entry_points={'pytest11': [
    'plugin_name=plugin.registration.module',
]}

因此