-
-
Notifications
You must be signed in to change notification settings - Fork 2.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
How to reuse same fixture implementation with different scopes? #3425
Comments
iriginally multi scoped fixtures was planned for pytest 3.0, but it demonstrated impossible to implement without a major refactoring, what you do there is the common workaround, but its suggested to use a contextmanager instead of yield from |
@RonnyPfannschmidt Thanks for such a prompt response. As per your suggestions of using contexmanager instead, do you mean something along these lines? @pytest.fixture(scope='module')
def my_fixture_mod():
with fixture_impl():
pass
@contextmanager
def fixture_impl():
print('BEFORE: Fixture implmentation')
yield
print('AFTER: Fixture implmentation') |
@jurisbu correct
|
Satisfactory answer received. Closing. |
Same idea can be applied to async fixtures/tests:
|
@RonnyPfannschmidt @jurisbu @iAnanich Is it possible to somehow trigger the test state to become Current example causes the unhandled exception and pytest process close:
|
I've been doing this: def _some_fixture(a_dependency_fixture):
def __some_fixture(x):
return x
yield __some_fixture
some_temp_fixture = pytest.fixture(_some_fixture, scope="function")
some_module_fixture = pytest.fixture(_some_fixture, scope="module")
some_session_fixture = pytest.fixture(_some_fixture, scope="session") Seemed more straightforward than a context manager. Any downsides to this? |
Currently we add an attribute to the function decorated by pytest/src/_pytest/fixtures.py Line 1197 in 611b579
So the fixtures above will actually point to the wrong definition... I'm surprised it works. |
Hello!
I am looking for a way to reuse same fixture or same fixture implementation in different scopes. I would like to avoid copy-pasting implementation in multiple fixtures with different scopes defined. What would be recommended approach?
So far I have thought about (ab)using
yield from
as in following example. Not sure if this can lead to some unexpected issues.Any other ideas or suggestions?
Thanks!
The text was updated successfully, but these errors were encountered: