Why does @pytest.fixture(autouse=True)
not populate namespace of test function?
#13172
Replies: 3 comments 5 replies
-
Moving this to a discussion, please use those for questions and only use issues for things that are actionable in some way. pytest doesn't do this because there is no way to magically "add something to the namespace" when you call a function. If you need the value of a fixture, you will need to have it as an argument. |
Beta Was this translation helpful? Give feedback.
-
Very simplistic example. >>> def foo():
... print(bar)
...
... exec(foo.__code__, {"bar": "baz"})
...
baz |
Beta Was this translation helpful? Give feedback.
-
@The-Compiler autoused = {arg: funcargs[arg] for arg in funcargs if arg not in testargs}
testfunction = types.FunctionType(testfunction.__code__, testfunction.__globals__ | autoused) Two caveats:
See #13180. |
Beta Was this translation helpful? Give feedback.
-
Hi everyone 👋
I'm quite new to
pytest
and I love it, so... thanks!! I recently understood -- or so I though -- how to use@pytest.fixture(autouse=True)
with the setup and teeardown logics separated withyield
.However, I was surprised to discover that the
yield
statement does not add the fixture to the namespace of the test function. For example, I expected the following to work.But as you probably know, it fails.
My questions are then:
pytest
not to make this work?Thanks for your time!
Beta Was this translation helpful? Give feedback.
All reactions