Search code examples
pytestfixtures

pytest: get current parametrize value from request fixture


I have a fixture that I want to apply to every test function, where I extract metadata from the tests. Something like

@pytest.fixture(autouse=True)
def extract_metadata(request):
    func_name = request.function.__name__
    # etc.
    ...

I also want to extract the parametrize values here. But I can't figure out how to extract the current parameter values from the request object. The only place I see that they are indicated at all is in the test id inside of the request.node.name, but I'd prefer to extract the actual values rather than parsing them out of the id in the string.


Solution

  • The parameters can be accessed with request.node.callspec.params, which is a dict mapping parameter name to parameter value.