Skip to content

Commit 45bb8b6

Browse files
committed
feat: Exposed autoused fixture names as global variable for tests (#13171)
Signed-off-by: Élie Goudout <[email protected]>
1 parent 2f36984 commit 45bb8b6

File tree

3 files changed

+10
-0
lines changed

3 files changed

+10
-0
lines changed

AUTHORS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,7 @@ Eduardo Schettino
141141
Edward Haigh
142142
Eero Vaher
143143
Eli Boyarski
144+
Élie Goudout
144145
Elizaveta Shashkova
145146
Éloi Rivard
146147
Emil Hjelm

changelog/13180.feature.rst

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
Exposed autoused fixture names as global variables for tests.
2+
3+
Two caveats:
4+
- It is still not possible to directly use, for example, `autoused_int_fixture += 1` without a prior `global autoused_int_fixture` statement in test definition.
5+
- This feature makes the test work on an augmented 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.

src/_pytest/python.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,10 @@ def pytest_pyfunc_call(pyfuncitem: Function) -> object | None:
150150
async_fail(pyfuncitem.nodeid)
151151
funcargs = pyfuncitem.funcargs
152152
testargs = {arg: funcargs[arg] for arg in pyfuncitem._fixtureinfo.argnames}
153+
autoused = {arg: funcargs[arg] for arg in funcargs if arg not in testargs}
154+
testfunction = types.FunctionType(
155+
testfunction.__code__, testfunction.__globals__ | autoused
156+
)
153157
result = testfunction(**testargs)
154158
if hasattr(result, "__await__") or hasattr(result, "__aiter__"):
155159
async_fail(pyfuncitem.nodeid)

0 commit comments

Comments
 (0)