24

Let's say I disabled a pytest plugin in my pytest.ini file like:

[pytest]
...
addopts=
    -p no:myplugin

Now I would like to be able to enable it sometimes with command line arguments, something like:

pytest -p yes:myplugin

Is that possible? Please, if you have better recommendations, I would like to know that too.

2 Answers 2

5

To load the plugin again, use -p pytest_myplugin. This will work when chained after -p no:myplugin (either on the command-line, or from pytest.ini's addopts).

What's happening here: when you specify -p no:plugin, pytest prepends "pytest_" to "plugin". This is because myplugin is actually imported from pytest_myplugin. Unfortunately, this convenience isn't mirrored on the loading side, which requires the full module name of the plugin.

2
  • 2
    At least in pytest 3.5.1 this doesn't work. When it parses -p no:foo the plugin is placed on the blocked list, and any following -p foo cannot re-enable it any-more. See here.
    – kleptog
    Commented Jun 14, 2018 at 12:38
  • Wow, yeah, this shouldn't have worked since at least 2.9. I've reproduced the irreproducibility at 3.0.0. It doesn't look like there's any built-in way to re-enable a blocked plugin from the command-line.
    – theY4Kman
    Commented Jun 14, 2018 at 15:26
-3
+100

I haven't ever needed to do this, as it is easier to disable plugins via commandline flags. As a workaround you can either specify a different ini file using the -c option and either have a different ini file or even use /dev/null as I have below

$ cat pytest.ini
[pytest]
addopts= -p no:django
$ py.test 
================================================= test session starts 
platform linux -- Python 3.4.3, pytest-3.0.5, py-1.4.32, pluggy-0.4.0
rootdir: /home/me/python, inifile: pytest.ini
plugins: pep8-1.0.6, cov-2.4.0
collected 0 items 
============================================ no tests ran in 0.02 seconds    
$ py.test -c /dev/null
================================================= test session starts
platform linux -- Python 3.4.3, pytest-3.0.5, py-1.4.32, pluggy-0.4.0
rootdir: /home/me/python, inifile: /dev/null
plugins: django-3.1.2, pep8-1.0.6, cov-2.4.0
collected 0 items 
============================================ no tests ran in 0.02 seconds

If you really need it, you could do something like. py.test -c <(grep -v no:django pytest.ini) using a unix namedpipe and use grep or sed to remove the plugin line. But it still seems easier to have all plugins by default and disable via commandline.

py.test -c <(grep -v no:django pytest.ini)
================================================= test session starts 
platform linux -- Python 3.4.3, pytest-3.0.5, py-1.4.32, pluggy-0.4.0
rootdir: /home/me/python, inifile: /dev/fd/63
plugins: django-3.1.2, pep8-1.0.6, cov-2.4.0
collected 0 items 
============================================ no tests ran in 0.03 seconds

Alternatively I would not specify addopts= -p no:myplugin in pytest.ini and instead use the PYTEST_ADDOPTS environment variable when I wanted to switch them off. But this is a slight reverse of what you asked for

5
  • thanks, yeah this is good to avoid the pytest.ini file, but a problem is that there could be already some plugins disabled there, and the idea is to only enable one in particular
    – eLRuLL
    Commented Feb 28, 2017 at 22:14
  • If you really need it, you could do something like. py.test -c <(grep -v no:django pytest.ini) using a unix named pipe and grep or sed to remove the plugins line. Commented Feb 28, 2017 at 22:22
  • Yeah, but the problem is really to make it simple for others running the test. I can avoid the file and disable each one individually on the command line, but that can't be the default way to run the tests in a project.
    – eLRuLL
    Commented Feb 28, 2017 at 22:39
  • It sounds like your use case might be complicated enough that you want a wrapper around the py.test command. Take a look at pytest.org/dev/… to generate a runtest.py which you can customize to add your own command or use pyinvoke.org which is what I use to wrap many commands apart from just tests. So you can hide all the command line options from your users and they just run a simple command. Commented Feb 28, 2017 at 22:45
  • yeah I am thinking on doing something like that. The whole idea is to keep everything configured so the tests could be run with only the pytest command, and then enable one plugin at a time if needed. Now I fixed this on my project by forking my own version of the plugin and adding a enabling argument.
    – eLRuLL
    Commented Feb 28, 2017 at 22:48

Not the answer you're looking for? Browse other questions tagged or ask your own question.