Search code examples
pythonpytestpytest-html

How to build a wrapper pytest plugin?


I want to wrap the pytest-html plugin in the following way:

  1. Add an option X
  2. Given the option X, delete data from the report

I was able to add the option with implementing the pytest_addoption(parser) function, but got stuck on the 2nd thing...

What I was able to do is this: implement a hook frmo pytest-html. However, I have to access my option X, in order to do what to do. The problem is, pytest-html's hook does not give the "request" object as a param, so I can't access the option value...

Can I have additional args for a hook? or something like this?


Solution

  • You can attach additional data to the report object, for example via a custom wrapper around the pytest_runtest_makereport hook:

    @pytest.hookimpl(hookwrapper=True)
    def pytest_runtest_makereport(item, call):
        outcome = yield
        report = outcome.get_result()
        report.config = item.config
    

    Now the config object will be accessible via report.config in all reporting hooks, including the ones of pytest-html:

    def pytest_html_report_title(report):
        """ Called before adding the title to the report """
        assert report.config is not None