23

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.

Tom Burrows
  • 2,225
  • 2
  • 29
  • 46
eLRuLL
  • 18,488
  • 9
  • 73
  • 99

2 Answers2

4

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.

theY4Kman
  • 5,572
  • 2
  • 32
  • 38
  • 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](https://github.com/pytest-dev/pytest/blob/4d0297b4138b694e1d99f4c8147aaf06d725d0f4/src/_pytest/config.py#L422-L429). – kleptog Jun 14 '18 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 Jun 14 '18 at 15:26
-3

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

dinosaurwaltz
  • 1,731
  • 1
  • 14
  • 12
  • 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 Feb 28 '17 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. – dinosaurwaltz Feb 28 '17 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 Feb 28 '17 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 http://pytest.org/dev/goodpractises.html#create-a-pytest-standalone-script to generate a `runtest.py` which you can customize to add your own command or use http://www.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. – dinosaurwaltz Feb 28 '17 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 Feb 28 '17 at 22:48