From 7a96d94fd4f98f725ce04f7ac45de041b521b86d Mon Sep 17 00:00:00 2001 From: Joshua Storck Date: Wed, 30 Oct 2019 14:18:13 -0400 Subject: Making it possible to access the pluginmanager in the pytest_addoption hook --- doc/en/example/markers.rst | 2 +- doc/en/example/parametrize.rst | 2 +- doc/en/example/simple.rst | 4 ++-- doc/en/writing_plugins.rst | 52 +++++++++++++++++++++++++++++++++++++++++- 4 files changed, 55 insertions(+), 5 deletions(-) (limited to 'doc') diff --git a/doc/en/example/markers.rst b/doc/en/example/markers.rst index ccddb1f66..8f81ff4d2 100644 --- a/doc/en/example/markers.rst +++ b/doc/en/example/markers.rst @@ -364,7 +364,7 @@ specifies via named environments: import pytest - def pytest_addoption(parser): + def pytest_addoption(parser, pluginmanager): parser.addoption( "-E", action="store", diff --git a/doc/en/example/parametrize.rst b/doc/en/example/parametrize.rst index 1220cfb4d..6f3e8031b 100644 --- a/doc/en/example/parametrize.rst +++ b/doc/en/example/parametrize.rst @@ -36,7 +36,7 @@ Now we add a test configuration like this: # content of conftest.py - def pytest_addoption(parser): + def pytest_addoption(parser, pluginmanager): parser.addoption("--all", action="store_true", help="run all combinations") diff --git a/doc/en/example/simple.rst b/doc/en/example/simple.rst index a7cd06d31..85000b46d 100644 --- a/doc/en/example/simple.rst +++ b/doc/en/example/simple.rst @@ -33,7 +33,7 @@ provide the ``cmdopt`` through a :ref:`fixture function `: import pytest - def pytest_addoption(parser): + def pytest_addoption(parser, pluginmanager): parser.addoption( "--cmdopt", action="store", default="type1", help="my option: type1 or type2" ) @@ -151,7 +151,7 @@ line option to control skipping of ``pytest.mark.slow`` marked tests: import pytest - def pytest_addoption(parser): + def pytest_addoption(parser, pluginmanager): parser.addoption( "--runslow", action="store_true", default=False, help="run slow tests" ) diff --git a/doc/en/writing_plugins.rst b/doc/en/writing_plugins.rst index 5f429c219..818974d0b 100644 --- a/doc/en/writing_plugins.rst +++ b/doc/en/writing_plugins.rst @@ -338,7 +338,7 @@ string value of ``Hello World!`` if we do not supply a value or ``Hello import pytest - def pytest_addoption(parser): + def pytest_addoption(parser, pluginmanager): group = parser.getgroup("helloworld") group.addoption( "--name", @@ -677,6 +677,56 @@ Example: print(config.hook) +.. _`addoptionhooks`: + + +Using hooks in pytest_addoption +------------------------------- + +Occasionally, it is necessary to change the way in which command line options +are defined by one plugin based on hooks in another plugin. For example, +a plugin may expose a command line option for which another plugin needs +to define the default value. The pluginmanager can be used to install and +use hooks to accomplish this. The plugin would define and add the hooks +and use pytest_addoption as follows: + +.. code-block:: python + + # contents of hooks.py + + # Use firstresult=True because we only want one plugin to define this + # default value + @hookspec(firstresult=True) + def pytest_config_file_default_value(): + """ Return the default value for the config file command line option. """ + + + # contents of myplugin.py + + + def pytest_addhooks(pluginmanager): + """ This example assumes the hooks are grouped in the 'hooks' module. """ + from . import hook + + pluginmanager.add_hookspecs(hook) + + + def pytest_addoption(parser, pluginmanager): + default_value = pluginmanager.hook.pytest_config_file_default_value() + parser.addoption( + "--config-file", + help="Config file to use, defaults to %(default)s", + default=default_value, + ) + +The conftest.py that is using myplugin would simply define the hook as follows: + +.. code-block:: python + + def pytest_config_file_default_value(): + return "config.yaml" + + Optionally using hooks from 3rd party plugins --------------------------------------------- -- cgit v1.2.3 From f4008042069b64111c4ad4ab047029a5e77f01ea Mon Sep 17 00:00:00 2001 From: Joshua Storck Date: Wed, 30 Oct 2019 16:25:50 -0400 Subject: Removing pluginmanager as parameter in definition of pytest_addoption hook --- doc/en/example/markers.rst | 2 +- doc/en/example/parametrize.rst | 2 +- doc/en/example/simple.rst | 4 ++-- doc/en/writing_plugins.rst | 2 +- 4 files changed, 5 insertions(+), 5 deletions(-) (limited to 'doc') diff --git a/doc/en/example/markers.rst b/doc/en/example/markers.rst index 8f81ff4d2..ccddb1f66 100644 --- a/doc/en/example/markers.rst +++ b/doc/en/example/markers.rst @@ -364,7 +364,7 @@ specifies via named environments: import pytest - def pytest_addoption(parser, pluginmanager): + def pytest_addoption(parser): parser.addoption( "-E", action="store", diff --git a/doc/en/example/parametrize.rst b/doc/en/example/parametrize.rst index 6f3e8031b..1220cfb4d 100644 --- a/doc/en/example/parametrize.rst +++ b/doc/en/example/parametrize.rst @@ -36,7 +36,7 @@ Now we add a test configuration like this: # content of conftest.py - def pytest_addoption(parser, pluginmanager): + def pytest_addoption(parser): parser.addoption("--all", action="store_true", help="run all combinations") diff --git a/doc/en/example/simple.rst b/doc/en/example/simple.rst index 85000b46d..a7cd06d31 100644 --- a/doc/en/example/simple.rst +++ b/doc/en/example/simple.rst @@ -33,7 +33,7 @@ provide the ``cmdopt`` through a :ref:`fixture function `: import pytest - def pytest_addoption(parser, pluginmanager): + def pytest_addoption(parser): parser.addoption( "--cmdopt", action="store", default="type1", help="my option: type1 or type2" ) @@ -151,7 +151,7 @@ line option to control skipping of ``pytest.mark.slow`` marked tests: import pytest - def pytest_addoption(parser, pluginmanager): + def pytest_addoption(parser): parser.addoption( "--runslow", action="store_true", default=False, help="run slow tests" ) diff --git a/doc/en/writing_plugins.rst b/doc/en/writing_plugins.rst index 818974d0b..8660746bd 100644 --- a/doc/en/writing_plugins.rst +++ b/doc/en/writing_plugins.rst @@ -338,7 +338,7 @@ string value of ``Hello World!`` if we do not supply a value or ``Hello import pytest - def pytest_addoption(parser, pluginmanager): + def pytest_addoption(parser): group = parser.getgroup("helloworld") group.addoption( "--name", -- cgit v1.2.3