微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

我在需要不同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 

都失败了,文档没有描述如何完成.任何指针非常感谢,谢谢!

解决方法:

反复传递-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_resources(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))

输出示例:

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',
]}

从而,

> the plugin name for pytest-splinter is pytest-splinter(duh!),
> the plugin name for pytest-django is django.

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐