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