-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
Allow broader scoped fixtures to rely on narrower scoped fixtures #797
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
Comments
I think the problem is that it would violate an invariant where fixture objects are the same independently where they were created. For example, it makes sense that both a function and fixture which use @pytest.fixture
def setup_with_files(tmpdir):
tmpdir.join('input.txt').write('...)
return MySetup()
def test_foo(tmpdir, setup_files):
setup_files.check()
contents = tmpdir.join('input.txt').read() That's the reason you can't use a narrow-scoped fixture as an input to a Hope this helps! 😄 |
Oh, that's interesting, and sort-of surprising. I would have expected in that use case that the fixture would have generated two different tmpdir objects from different invocations of the fixture. I see how it might be convenient to be able to demand the same fixture, but that behavior strikes me as dangerous and implicit. What if one wants a different tmpdir? Consider if a function demands a Furthermore, it would be possible for any fixture, such as
or
It seems to me there's no clean way to implement the use case I've described in the original post. Given that the intended functionality is possible without re-using fixtures, and given that the implicit re-use of fixtures is sometimes surprising and prevents the elevation of scope for a fixture, I would strongly consider changing that expectation. I do note that what I'm suggesting is a potentially drastic change in expectation and probably substantial changes to the implementation. This leads to a few questions. Is there another workaround for my use case? Am I wrong in thinking fixtures would be more flexible if not implicitly re-used (with minimal loss through explicit re-use)? Would the scope and impact of this kind of change be too great to consider it for a future (incompatible) release? |
I see you point, but I personally disagree. When I discovered that fixtures re-used the same object, I thought "well that's interesting!". 😄 You example makes sense for Either way, as you noted, this change is too drastic and would certainly break a lot of test suites out there (my own test suites, and I suspect many many others, rely on this behavior). Perhaps we could add this as an option (perhaps passing an optional argument to
I think the workaround you proposed is good enough... if one changes the implementation to an
I'm of the opinion that it would make fixtures less flexible and more dependent on one another... but that's my opinion, others may of course disagree. 😄
I would say certainly for the 2.X series, as it would severely impact existing test suites, unless implemented in way that is optional like I mentioned above. |
for tmpdir there is a simple rough plan already, we want that the tmpdir_manager could be used to ask for all tmpdirs materialized in the parent item chain for a item |
Just to add here, in |
Closing this one as done |
I have this scenario: a plugin provides a fixture for a resource:
In my application, I want to do some heavy processing on cheap thing and create an expensive thing. Because it's an expensive thing, I want it to be session scoped:
However, the scoping rules disallow this usage. I can understand how the implementation may have gotten to a place where the scope processing might preclude naturally using a function-scoped fixture in a session-scoped fixture. However, I don't see any reason why the function-scoped fixture couldn't be invoked to be utilized in this way, where the function-scoped fixture is invoked and that instance of the fixture scheduled for teardown at the close of the session scope.
It might be possible to refactor the functionality like so:
Except that presumes things about the behavior of cheap_thing. If it were changed to a yield_fixture, for example, that would break the workaround.
Is there something more intrinsic that would preclude the behavior proposed above? Would it add undue complexity?
Thanks for the consideration.
The text was updated successfully, but these errors were encountered: