I am trying to create a fixure that simply prints the arguments of a pytest test case.
For example:
@pytest.fixture(scope='function')
def print_test_function_arguments(request):
# Get the value of argument_1 from the run of the test function
print(f'argument_1 = {value_1}')
def test_something(print_test_function_arguments, argument_1, argument_2):
assert False
If you want to do any kind of introspection, request
fixture is the way to go. request.node
gives you the current test item, request.node.function
the test_something
function object and request.getfixturevalue("spam")
will evaluate the fixture spam
and return its result (or take it from fixture cache if already evaluated before). A simple args introspection example (untested):
import inspect
import pytest
@pytest.fixture(scope='function')
def print_test_function_arguments(request):
argspec = inspect.getfullargspec(request.node.function)
positional_args = argspec.args
positional_args.remove("print_test_function_arguments")
for argname in positional_args:
print(argname, "=", request.getfixturevalue(argname))
Of course, you can't evaluate the fixture print_test_function_arguments
in its body, otherwise it will stuck in an infinite recursion, so its name must be removed from arguments list first.