Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to disable multiple plugins in 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 

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!

like image 786
Liz Avatar asked Aug 28 '18 02:08

Liz


People also ask

What is Pytest_plugins?

Plugins are generally files defined in your project or other modules which might be needed in your tests. You can also load a set of predefined plugins as explained here. pytest_plugins = "someapp.someplugin" Hooks: You can specify hooks such as setup and teardown methods and much more to improve your tests.


1 Answers

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

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
...

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))

Example output:

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

Thus,

  • the plugin name for pytest-splinter is pytest-splinter (duh!),
  • the plugin name for pytest-django is django.
like image 139
hoefling Avatar answered Sep 18 '22 13:09

hoefling