numirias / pytest-json-report

🗒️ A pytest plugin to report test results as JSON
MIT License
147 stars 39 forks source link

pytest_json_modifyreport hook add session as input param #61

Closed 15klli closed 3 years ago

15klli commented 3 years ago
numirias commented 3 years ago

You can get missing information via other hooks. If you need the session object, take it from pytest_sessionstart, e.g.:

state = {}

def pytest_sessionstart(session):
    state['session'] = session

def pytest_json_modifyreport(json_report):
    json_report['startdir'] = state['session'].startdir

Many pytest plugins store a reference to the current session in a similar way, e.g.:

class MyPlugin:

    def __init__(self, ...):
        self._session = None

    def pytest_sessionstart(self, session):
        self._session = session

    ... # access the session via self._session when needed

I'm reluctant to add session to the hookspec args as it seems somewhat arbitrary since report, config, etc. may be just as useful in other cases.