Skip to content

Commit

Permalink
feat: Exposed autoused fixture names as global variable for tests (#1…
Browse files Browse the repository at this point in the history
…3171)

Signed-off-by: Élie Goudout <[email protected]>
  • Loading branch information
eliegoudout committed Jan 31, 2025
1 parent 2f36984 commit 698f8ea
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 0 deletions.
1 change: 1 addition & 0 deletions AUTHORS
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,7 @@ Eduardo Schettino
Edward Haigh
Eero Vaher
Eli Boyarski
Élie Goudout
Elizaveta Shashkova
Éloi Rivard
Emil Hjelm
Expand Down
5 changes: 5 additions & 0 deletions changelog/13180.feature.rst
Original file line number Diff line number Diff line change
@@ -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.
4 changes: 4 additions & 0 deletions src/_pytest/python.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 698f8ea

Please sign in to comment.