pytest

Pytest pluggy._manager.PluginValidationError due to the pytest-json-report plugin


I use pytest together with the pytest-json-report plugin. I have the pytest_json_modifyreport hook in the conftest.py file.

When I run the command pytest --json-report, it is OK. But when I run the simple pytest command, it yields the following: pluggy._manager.PluginValidationError: unknown hook 'pytest_json_modifyreport' in plugin.

Is it possible to get rid of that error without commenting out the hook?


Solution

  • According to the official documentation:

    A note on hooks

    If you're using a pytest_json_* hook although the plugin is not installed or not active (not using --json-report), pytest doesn't recognize it and may fail with an internal error like this:

    INTERNALERROR> pluggy.manager.PluginValidationError: unknown hook 'pytest_json_runtest_metadata' in plugin <module 'conftest' from 'conftest.py'>
    

    You can avoid this by declaring the hook implementation optional:

    import pytest
    @pytest.hookimpl(optionalhook=True)
    def pytest_json_runtest_metadata(item, call):
        ...