diff --git a/AUTHORS b/AUTHORS index e670571566a..c2639434ca0 100644 --- a/AUTHORS +++ b/AUTHORS @@ -141,6 +141,7 @@ Eduardo Schettino Edward Haigh Eero Vaher Eli Boyarski +Élie Goudout Elizaveta Shashkova Éloi Rivard Emil Hjelm diff --git a/changelog/13180.feature.rst b/changelog/13180.feature.rst new file mode 100644 index 00000000000..87b9bc1c33b --- /dev/null +++ b/changelog/13180.feature.rst @@ -0,0 +1,5 @@ +Exposed autoused fixture names as global variables for tests. + +Two caveats: +- It is still not possible to directly use, for example, `autoused_int_fixture += 1` without a prior `global autosed_int_fixture` statement in test definition. +- This feature makes the test work on a copy of its `.__globals__` dict. So if the test actually modifies `globals()` (which looks like a terrible idea and is probably discouraged somewhere in pytest docs), it may cause issues. diff --git a/src/_pytest/python.py b/src/_pytest/python.py index ef8a5f02b53..0b11e716a1f 100644 --- a/src/_pytest/python.py +++ b/src/_pytest/python.py @@ -150,6 +150,10 @@ def pytest_pyfunc_call(pyfuncitem: Function) -> object | None: async_fail(pyfuncitem.nodeid) funcargs = pyfuncitem.funcargs testargs = {arg: funcargs[arg] for arg in pyfuncitem._fixtureinfo.argnames} + autoused = {arg: funcargs[arg] for arg in funcargs if arg not in testargs} + testfunction = types.FunctionType( + testfunction.__code__, testfunction.__globals__ | autoused + ) result = testfunction(**testargs) if hasattr(result, "__await__") or hasattr(result, "__aiter__"): async_fail(pyfuncitem.nodeid)